简体   繁体   English

Python使用变量和if语句

[英]Python using variables and if statements

I am doing an assignment where I have to conduct a quiz. 我正在做一个作业,我必须进行测验。 Here 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:")  #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!")

The problem i'm having is on line 28, where it says answerb is not defined but i did define it. 我遇到的问题在第28行,其中说answerb尚未定义,但我确实定义了它。 I am supposed to do this quiz with terms that I have learnt and I am not allowed to use terms i haven't learnt such as while. 我应该使用我已经学过的术语来进行此测验,并且不允许我使用诸如一段时间内未学到的术语。 I put else: print incorrect and put answerb == input to give the user a 2nd chance at answering. 我输入其他内容:打印错误,并输入answerb ==输入,以使用户有第二次回答的机会。 And if they get it the first try, they dont need to insert something for answerb. 并且,如果他们第一次尝试,就不需要为answerb插入内容。

This line... 这条线...

    answerb = input("Your answer: ")

is indented too far. 缩进太远。 It will only be called in the 'else' portion of the preceding if statement. 它只会在前面的if语句的“ else”部分中调用。

answerb = input("Your answer: ")

Unindent it so that it's at the same level as the following if , so that it will always be called (and thus answerb will always be defined). 取消缩进使其与以下if处于同一级别,以便始终对其进行调用(因此将始终定义answerb )。

Edit: 编辑:

Going by your comment as well as your code structure, I think you want to indent this part: 根据您的注释以及代码结构,我认为您希望缩进这一部分:

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

Your code should look like this then: 您的代码应如下所示:

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. No, you haven't defined answerb properly. 不,您没有正确定义answerb It is within else statement which may not have been executed. 它位于else语句中,可能尚未执行。 I assume what you want is to indent your last if statement because it is executed only if answer is wrong. 我假设您要缩进最后一个if语句,因为仅当答案错误时才执行该语句。

  2. Instead of multiple question == use question in ['bla1', 'bla2', 'bla3'] . 代替多个question == question in ['bla1', 'bla2', 'bla3']使用question in ['bla1', 'bla2', 'bla3'] Here you don't even need this, since you don't care about capitalization. 在这里,您甚至不需要这个,因为您不必担心大小写。 You may do question.lower() == 'bla bla bla' which will match all possible capitalizations. 您可以执行question.lower() == 'bla bla bla' ,它将匹配所有可能的大写字母。 (same for other if 's) (同样用于其它if “S)

  3. The structure of your program is horrible. 您的程序的结构是可怕的。 You need to rewrite it using loops and store your questions and answers in array. 您需要使用循环将其重写并将问题和答案存储在数组中。

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

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