vendredi 22 septembre 2017

Repeating for loop within a function Python 3

I have been working on this problem for an Intro to Programming with Python class for days now. The goal is to create a program that generates random movie titles from a list of user-inputed words. The output should look something like this:

Please enter a list of words: ["Space", "Show", "Adventure", "Story", "Love", "Wild", "Life"]

Please enter a number of movies you'd like to generate: 3

Welcome to Randoplex! Currently playing movies are:

Adventure Life Space

Show Love Story

Wild Space Life

I have been able to generate random titles, but cannot find anyway to generate different titles based on the user-inputed number. This is the closest I have gotten:

import random

def random_movies(word_list = eval(input("Please enter a list of words:"))):

for word in word_list:
    titles = ' '.join(random.choice(word_list) for i in range(3))
    return titles

def repeat():

  titles = random_movies()
  movie_num = eval(input("Please enter the number of movies you'd like to  generate:"))

 for i in range(movie_num):
    random_movies()
    print(titles)

titles1 = repeat()
print("Welcome to Randoplex! Currently playing movies are:")
print()
print(titles1)

This allows me to print the same randomized title for the value movie_num that the user enters. But, I just cannot figure out how to create multiple different randomized movie titles. I have tried using the range() function in many different ways, and have looked all over the internet for advice. I truly don't know what else to try at this point, any help would be greatly appreciated. It is important to mention that my class is very elementary, we haven't been introduced to "while" loops yet and I don't think I am allowed to use them because of that. Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire