-1

I'm doing an assignment where I have to conduct a quiz for different topics. This is my code so far.

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")
print("a) Video Games")
print("b) Soccer")
print("c) Geography") 
choice = input("Which topic would you like to begin with?")
if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)':
    print("You picked Video Games.")
print("Question number one:")
print("What is the most popular FPS (First Person Shooter) game?")
print("a) Call of Duty")
print("b) Battlefield")
print("c) Grand Theft Auto 5")
print("d) Counter Strike")
answer = input("Your answer:")
guessesTaken = 0
if answer == 'Call Of Duty' or answer == 'Call of duty' or answer == 'Call of duty' or answer == 'a)' or answer == 'call of duty':
    print("You are correct!")
else: 
    guessesTaken = guessesTaken + 1
    print("Incorrect!")
    print("You have", guessesTaken, "guess left!")

I am trying to make it so that if they get the answer wrong, they get another chance at answering the question. Right now once they get it wrong, they can't type again. Thanks!

4

2 回答 2

1

这是一个简单且经常遇到的问题。该解决方案通常适合这种情况:

flag = False

while not flag:
    x = input("Get input from the user:")

    if validate(x):
        flag = True
    else:
        print "Input invalid. Try again"

变量名称当然应该更改为适合当前任务(例如flag-->answerCorrect或类似,x-->answer等)。

于 2013-10-28T00:30:12.900 回答
1

你应该像@BartoszKP 所说的那样,使用一个while循环来检查用户是否输入了一个有效的输入。

话虽如此,我有一些建议可以提高代码的可读性。而不是这个

if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)':
    print("You picked Video Games.")

您可以利用str().lower()方法:

if choice.lower() == 'video games' or choice == 'a':
    print('You picked Video Games.")

lower() 方法将所有字母转换为小写。

关于 while 循环,我不喜欢使用标志变量——它在代码中添加了一个不需要的额外变量。相反,你可以利用break

while True:
    choice = input('Which topic would you like to begin with?')
    if choice.lower() == 'video games' or 'a':
        print('You picked Video Games.')
        break #This breaks out of the while loop, and continues executing the code that follows the loop

另一种解决方案是choice在循环之前定义变量while,并运行它直到输入符合您的要求:

choice = input('Which topic would you like to begin with?')
while choice.lower() != 'video games' and choice != 'a':
    print('Please pick a valid option')
    choice = input('Which topic would you like to begin with?')
print('You picked "{}".'.format(choice))

如果您希望能够在不同的选项之间进行选择,可以通过检查输入字符串是否是列表中的项目之一来进一步改进代码:

valid_options = ['video games', 'a', 'software', 'b', 'cartoons', 'c']
choice = input('Which topic would you like to begin with?')
while choice.lower() not in valid_options:
    print('Please pick a valid option')
    choice = input('Which topic would you like to begin with?')
print('You picked "{}".'.format(choice))

输出:

Which topic would you like to begin with?Movies
Please pick a valid option
Which topic would you like to begin with?vIdeO gaMes
You picked "vIdeO gaMes".

Which topic would you like to begin with?software
You picked "software".

如果您使用 Python 2.x,您还应该考虑使用raw_input()而不是input(). 请参阅这个相关的 SO 问题以了解原因。

于 2013-10-28T00:50:53.907 回答