Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. # Functions go here
  2.  
  3.  
  4. # Offers users list of options, can be set to choose a random response
  5. def text_helper(question, possible_answers, items_per_line, required=None):
  6. error = "This is a required field. Please choose an item from the list \n"
  7. "or type in a word / phrase"
  8. print(question)
  9.  
  10. valid = False
  11. while not valid:
  12. # Output numbered list with specified number of items per line
  13. count_ans = 0
  14. for item in possible_answers:
  15. print("{}. {}".format(possible_answers.index(item)+1, item),
  16. end="\t")
  17. count_ans += 1
  18. if count_ans % items_per_line == 0:
  19. print()
  20. print()
  21. response = input("choose: ").lower()
  22.  
  23. # Checks that response has been given if required
  24. if required == "yes" and response == "":
  25. print(error)
  26. continue
  27.  
  28.  
  29. # check if response is a number / choice, if it is, change
  30. # it to the option in the list
  31.  
  32. try:
  33. response = int(response) - 1
  34. # Check that number is an option!
  35. if 0 <= int(response) < len(possible_answers):
  36. # response is a choice in the list, return word
  37. # that matches chose number
  38. response = possible_answers[response]
  39. return response
  40.  
  41. else:
  42. print(error)
  43.  
  44. except ValueError:
  45. # User has typed in text, return it to the main routine
  46. return response
  47.  
  48. # Main Routine
  49.  
  50.  
  51. # *** Set up genre list and sort it ***
  52. genre_list = ["Action", "Adventure" , "Detective", "Dystopian", "Crime", "Fan-Fiction", "Mystery",
  53. "Science Fiction (Sci-Fi)", "Western", "Steam Punk", "Drama", "Horror", "Suspense",
  54. "Thriller", "Romance", "Chic Lit", "Humor", "Satire", "Classic", "Historical Fiction",
  55. "Realistic Fiction", "narrative", "biography", "autobiography", "memoir", "Anthology",
  56. "Short Story", "Comic", "Graphic Novel", "Fable", "Fairy Tale", "Fantasy", "Legend",
  57. "Magical Realism", "Mythology"]
  58.  
  59. genre_list.sort()
  60. print(genre_list)
  61.  
  62. # Make everything in list lowercase for comparison purposes
  63. genre_list = [x.lower() for x in genre_list]
  64.  
  65. valid_genre = False
  66. while not valid_genre:
  67. genre = text_helper("Please choose a genre", genre_list, 4, "yes" )
  68.  
  69. if genre in genre_list:
  70. valid_genre = True
  71.  
  72. print("Genre: {}".format(genre))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement