我在python测验代码中输入得分时遇到问题。这是脚本:
#This is the Test script for )TUKT(, Developed by BOT.
print ""
print "Welcome to the Ultimate Kirby Test."
print ""
begin = (raw_input("Would you like to begin?:"))
if begin == "yes":
print ""
print "Alright then! Let's start, shall we?"
print "Q1. What color is Kirby?"
print "1) Black."
print "2) Blue."
print "3) Pink."
print "4) Technically, his color changes based on the opponent he swallows."
choice = input("Answer=")
if choice ==1:
print "Incorrect."
print ""
elif choice ==2:
print "Incorrect."
print ""
elif choice ==3:
print "Tee hee.... I fooled you!"
print ""
elif choice ==4:
score = score+1
print "Well done! You saw through my trick!"
print ""
elif choice > 3 or choice < 1:
print "That is not a valid answer."
print ""
print "Well done! You have finished my test quiz."
print("Score:")
print ""
#End of Script
错误总是说 得分=得分+ 1未定义。 我没有得到任何研究。 谢谢!非常感谢帮助!
答案 0 :(得分:2)
您忘了定义名为score
的变量。您不能引用不存在的值!
只需在开始时声明:
score = 0
在score = score + 1
行中,Python会:'所以我需要创建一个名为score
的变量。它包含score
的值,加上1
。'但score
尚不存在,因此会引发错误。
答案 1 :(得分:0)
从未定义变量score
。插入score = 0
作为脚本的第一行。