如何使python从列表中返回尽可能多的谜语



我是一个完全的初学者,想要创造一个简单的谜语游戏,但我希望用户可以选择他想要多少谜语。现在我试图使用'for'函数,但我认为我搞砸了,有什么建议吗?我当前的代码:

import random
random_riddle_list = ('If an airplane crashed exactly on the border of the United States and Canada, where would the survivors be buried?'  ,
'What does an orange do when it takes a rest? ' , 'A cowboy rides in a town on Friday stays for three days and leaves on Friday. How is that possible? ' ,
'What disappears the instant you say its name? ' , 'Which berry makes a harsh noise? ' , 'How do you tell a boy snowman apart from a girl snowman? ' ,
'You walk a mile south, a mile east and then a mile north. You end up in exact same spot you started. Where are you? ' , 'What connects two people but touches only one? ')
riddle_number = str(len(random_riddle_list))
riddles_wanted = str(input("How many riddles do you want?(max 8): "))
for riddle_number in riddles_wanted:
random_riddle = random.choice(random_riddle_list)
answer = input(random_riddle).lower()
if answer == "You don’t bury survivors":
print("Correct")
elif answer == "Concentrates":
print("Correct")
elif answer == "His horse is named Friday":
print("Correct")
elif answer == "Silence":
print("Correct")
elif answer == "Raspberry":
print("Correct")
elif answer =="Snowballs":
print("Correct")
elif answer =="The North Pole":
print("Correct")
elif answer =="A wedding ring":
print("Correct")
else:
print("incorrect")

欢迎Matthew!你可以在下面找到一个建议。

创建一个谜语答案列表将允许您在代码的答案检查部分减少冗长。我还建议使用random.sample来代替random.choice,这样你就不会得到重复的谜语。

import random
random_riddle_list = ['If an airplane crashed exactly on the border of the United States and Canada, where would the survivors be buried?'  ,
'What does an orange do when it takes a rest? ' , 'A cowboy rides in a town on Friday stays for three days and leaves on Friday. How is that possible? ' ,
'What disappears the instant you say its name? ' , 'Which berry makes a harsh noise? ' , 'How do you tell a boy snowman apart from a girl snowman? ' ,
'You walk a mile south, a mile east and then a mile north. You end up in exact same spot you started. Where are you? ' , 'What connects two people but touches only one? ']
riddle_answers = ["You don’t bury survivors", "His horse is named Friday", "Silence", "Raspberry", "Snowballs", "The North Pole", "A wedding ring"]
riddles_wanted = int(input("How many riddles do you want?(max 8): "))
random_riddles_numbers = random.sample(population = range(len(random_riddle_list)),k=riddles_wanted)
for n in random_riddles_numbers:
answer = input(random_riddle_list[n]).lower()
if answer == riddle_answers[n].lower():
print("Correct")
else:
print("Incorrect")

最新更新