2013-10-28 67 views
-1

我正在做一个任务,我必须为不同的主题进行测验。这是我的代码到目前为止。Python编程循环

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!") 

我试图让这个,如果他们得到的答案错了,他们得到的回答这个问题的另一个机会。现在一旦他们弄错了,他们不能再打字了。谢谢!

+5

。提示:'while'。 – Kevin

回答

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.") 

下()方法将所有字母为小写。

关于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 

另一种解决方案是while循环之前定义choice变量,并运行它,直到输入是像你想:

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()。请参阅this相关的SO问题来理解为什么。

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等)。

+0

在我的情况下,我需要在哪里插入?我现在有点困惑。 –

+1

@ShahaadBoss只需停下一会儿,试着慢慢思考。循环重复行为。你的情况需要重复什么?所有需要重复的内容都应该在一个循环内。您需要多次提问,并多次验证。所以,至少提出问题并验证它应该是循环的。 之后,请务必仔细检查是否有可能完成循环(在这种情况下:“flag”是否有机会设置为“True”,以便循环不会无限)。为了简单起见,考虑将你的代码分割成函数。 – BartoszKP