输入得分(python 2.7.5)

时间:2014-03-31 20:13:18

标签: python

我在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未定义。 我没有得到任何研究。 谢谢!非常感谢帮助!

2 个答案:

答案 0 :(得分:2)

您忘了定义名为score的变量。您不能引用不存在的值!

只需在开始时声明:

score = 0

score = score + 1行中,Python会:'所以我需要创建一个名为score的变量。它包含score的值,加上1。'score尚不存在,因此会引发错误。

答案 1 :(得分:0)

从未定义变量score。插入score = 0作为脚本的第一行。