Python程式设计循环

Python程式设计循环

本文介绍了Python程式设计循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个作业,我必须针对不同的主题进行测验.到目前为止,这是我的代码.

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!

推荐答案

您应该按照@BartoszKP的说明进行操作,使用while循环检查用户是否输入了有效的输入.

You should do as @BartoszKP says, use a while loop to check that the user has entered a valid input.

话虽如此,我有一些建议可能会提高代码的可读性.代替这个

That being said, I have a few recommendations that might improve the readability of your code.Instead of this

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()方法将所有字母转换为小写字母.

The lower() method converts all letters to lower-case.

关于while循环,我不喜欢使用标志变量-它向代码中添加了一个不需要的额外变量.相反,您可以使用break

Regarding the while loop, I don't like to use a flag variables - it adds an extra variable to the code that isn't really needed. Instead, you could make use of 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变量,并运行它直到输入像您想要的那样:

Another solution is to define the choice variable before the while loop, and run it until the input is like you want:

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

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

If you want to be able to choose between different options, the code could be further improved by checking if the input string is one of the items in a list:

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问题了解原因.

If you use Python 2.x, you should also consider using raw_input() instead of input(). Please see this related SO question to understand why.

这篇关于Python程式设计循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:43