【问题标题】:Python Programming LoopPython 编程循环
【发布时间】:2013-10-28 00:25:24
【问题描述】:

我正在做一个任务,我必须针对不同的主题进行测验。这是我目前的代码。

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

我正在努力做到这一点,以便如果他们回答错误,他们将有另一个机会回答这个问题。现在一旦他们弄错了,他们就不能再打字了。谢谢!

【问题讨论】:

    标签: python loops


    【解决方案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
    

    另一种解决方案是在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 问题以了解原因。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 在我的情况下,我必须在哪里插入?我现在有点困惑。
      • @ShahaadBoss 稍等片刻,慢慢思考。循环重复行为。在您的情况下需要重复什么?需要重复的所有内容都应该在一个循环中。您需要多次提出问题,并多次验证。因此,至少提出问题并验证它应该是一个循环。之后,请务必仔细检查循环是否有可能完成(在这种情况下:flag 是否有机会设置为True,因此循环不会是无限的)。为了让事情变得更简单,请考虑将您的代码划分为函数。
      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多