繁体   English   中英

Python使用变量和if语句

[英]Python using variables and if statements

我正在做一个作业,我必须进行测验。 到目前为止,这是我的代码。

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")  #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)' or choice == 'a':
    print("You picked Video Games.")
print("Question number one:")                                      #Question 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")       
maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
question = input("Your answer: ")
if question == 'Call of duty' or question == 'call of duty' or question == 'Call Of Duty' or question == 'Call of Duty' or question == 'a' or question == 'a)':
    print("You are correct! You guessed the question on the first try!")
    points = points + maxGuesses-guessesTaken
    print("You scored",(maxGuesses-guessesTaken), "points!")
else:
    print("Incorrect!")
    print("You have", (maxGuesses-guessesTaken-1), "guesses remaining!")
    answerb = input("Your answer: ")
if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
    print("You are correct!")
    points = points + maxGuesses-guessesTaken
    print("You scored", (maxGuesses-guessesTaken-1), "points!")

我遇到的问题在第28行,其中说answerb尚未定义,但我确实定义了它。 我应该使用我已经学过的术语来进行此测验,并且不允许我使用诸如一段时间内未学到的术语。 我输入其他内容:打印错误,并输入answerb ==输入,以使用户有第二次回答的机会。 并且,如果他们第一次尝试,就不需要为answerb插入内容。

这条线...

    answerb = input("Your answer: ")

缩进太远。 它只会在前面的if语句的“ else”部分中调用。

answerb = input("Your answer: ")

取消缩进使其与以下if处于同一级别,以便始终对其进行调用(因此将始终定义answerb )。

编辑:

根据您的注释以及代码结构,我认为您希望缩进这一部分:

if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
    print("You are correct!")
    points = points + maxGuesses-guessesTaken

您的代码应如下所示:

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")  #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)' or choice == 'a':
    print("You picked Video Games.")
print("Question number one:")                                      #Question 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")       
maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
question = input("Your answer: ")
if question == 'Call of duty' or question == 'call of duty' or question == 'Call Of Duty' or question == 'Call of Duty' or question == 'a' or question == 'a)':
    print("You are correct! You guessed the question on the first try!")
    points = points + maxGuesses-guessesTaken
    print("You scored",(maxGuesses-guessesTaken), "points!")
else:
    print("Incorrect!")
    print("You have", (maxGuesses-guessesTaken-1), "guesses remaining!")
    answerb = input("Your answer: ")
    if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
        print("You are correct!")
        points = points + maxGuesses-guessesTaken
    print("You scored", (maxGuesses-guessesTaken-1), "points!")
  1. 不,您没有正确定义answerb 它位于else语句中,可能尚未执行。 我假设您要缩进最后一个if语句,因为仅当答案错误时才执行该语句。

  2. 代替多个question == question in ['bla1', 'bla2', 'bla3']使用question in ['bla1', 'bla2', 'bla3'] 在这里,您甚至不需要这个,因为您不必担心大小写。 您可以执行question.lower() == 'bla bla bla' ,它将匹配所有可能的大写字母。 (同样用于其它if “S)

  3. 您的程序的结构是可怕的。 您需要使用循环将其重写并将问题和答案存储在数组中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM