我必须创建一个关于农业的基本3问题测验。它需要询问3个问题,输出你是否正确或不正确,如果你弄错了,你可以再试一次。它还需要有分数功能。我已经完成了问题以及规范中不正确/正确的部分,但无论我尝试什么,我都无法使得分数功能起作用。我试过了:
score = 0
def counter(score)
score = score + 1
def counter(score)
score = 0
score = score + 1
def counter(score)
global score
score = 0
score = score + 1
然后一旦答案是正确的,该行读到:
counter(score)
我也试过
score = 0
然后
score = score + 1
但没有任何工作,我无法弄清楚出了什么问题。它还需要打印最终用户的数量。
CODE:
score = 0
def quiz():
print("Here is a quiz to test your knowledge of farming...")
print()
print()
print("Question 1")
print("What percentage of the land is used for farming?")
print()
print("a. 25%")
print("b. 50%")
print("c. 75%")
answer = input("Make your choice: ")
if answer == "c":
print("Correct!")
score = score + 1
else:
print("Incorrect.")
answer = input("Try again! ")
if answer == "c":
print("Correct")
score = score + 1
else:
print("Incorrect! Sorry the answer was C.")
print()
print()
print("Question 2")
print("Roughly how much did farming contribute to the UK economy in 2014.")
print()
print("a. £8 Billion.")
print("b. £10 Billion.")
print("c. £12 Billion.")
answer = input("Make your choice: ")
if answer == "b":
print("Correct!")
score = score + 1
else:
print("Incorrect.")
answer = input("Try again! ")
if answer == "b":
print("Ccrrect!")
score = score + 1
else:
print("Incorrect! Sorry the answer was B.")
print()
print()
print("Question 3.")
print("This device, which was invented in 1882 has revolutionised farming. What is it called?")
print()
print("a. Tractor")
print("b. Wagon.")
print("c. Combine.")
answer == input("Make your choice. ")
if answer == "a":
print("Correct!")
score = score + 1
else:
print("Incorrect.")
answer == input("Try again! ")
if answer == "a":
print("Correct!")
score = score + 1
else:
print("Incorrect! Sorry the answer was A.")
print("You got {0}/3 right!".format(score))
答案 0 :(得分:1)
n00b(和工作)的方式是做类似
的事情score = 0
def quiz():
global score
global
关键字使用全局变量(您声明在之外的函数quiz
)。由于您没有正确缩进代码,因此您不清楚是否要在quiz
函数内部或外部打印最后一个语句,但这并不重要。如果你同时放置score
并在测验函数内打印,或者两者都打印在你之外就可以了。祝你的家庭作业好运!
答案 1 :(得分:0)
因此python范围由缩进定义(如上面的注释所述)。因此,创建一个范围层的任何语句都由缩进分隔。示例包括类,函数,循环和布尔语句。这是一个例子。
Class A:
def __init__(self, msg):
self.message = msg
def print_msg(self): # prints your message
print self.message
def is_hi(self):
if self.message == "hi":
return true
else:
return false
这显示了可以存在的不同范围。 您的代码现在无法正常工作,因为您正在定义一个函数,然后不在其范围内放置任何内容。你必须在该函数的范围内放置任何东西才能消除该错误,但这可能不是你想要的。
答案 2 :(得分:0)
import sys
def quiz():
score = 0
print("Here is a quiz to test your knowledge of farming...")
print()
print()
print("Question 1")
print("What percentage of the land is used for farming?")
print()
print("a. 25%")
print("b. 50%")
print("c. 75%")
answer = input("Make your choice: ")
if answer == "c":
print("Correct!")
score = score + 1
else:
print("Incorrect! Sorry the answer was C.")
print()
print()
print("Question 2")
print("Roughly how much did farming contribute to the UK economy in 2014.")
print()
print("a. £8 Billion.")
print("b. £10 Billion.")
print("c. £12 Billion.")
answer = input("Make your choice: ")
if answer == "b":
print("Correct!")
score = score + 1
else:
print("Incorrect! Sorry the answer was B.")
print()
print()
print("Question 3.")
print("This device, which was invented in 1882 has revolutionised farming. What is it called?")
print()
print("a. Tractor")
print("b. Wagon.")
print("c. Combine.")
answer = input("Make your choice: ")
if answer == "a":
print("Correct!")
score = score + 1
else:
print("Incorrect! Sorry the answer was A.")
print("You got {}/3 right!".format(score))
def main():
quiz();
if __name__ == "__main__":
main()
因此,请比较您的代码和此代码,并查看我所做的更改,我希望这会对您有所帮助。在我走之前的一些事情:
了解'='和'=='之间的区别。 '='是赋值运算符,您将变量设置为某个值。 '=='用于测试目的,例如如果5 == 3 + 2:打印'真' 我注意到你的一些if语句有这个问题。
如果你有'If 名称 ==“主要”:main()'代码,那么最后的主要功能将会运行。< / p>