修复评分系统

时间:2014-11-20 18:23:55

标签: python

为什么这不起作用 我是python的新手,只是尝试创建一个数学测验,当我运行此代码时,我得到语法错误 得分未定义

import random
print ("Welcome to Math Extreme PRE EARLY PRE-ALPHA")
def AskAQuestion1():
    global score
    Number1 = random.randint(0,10)
    Number2 = random.randint(0,10)
    print ("Calculate"+ str(Number1) + "+"+ str(Number2)+"")
    answer = input ()
    answer = int (answer)
    if answer == Number1+Number2:
         print ("Correct")
         score = score + 1
    else:
         print ("wrong")

print ("Pick A Level")
print ("1 = Level 1")
answer = input ()
answer = int (answer)
if answer == 1:
    print (" Ok the test will start")
    for repeat in range (100):
        AskAQuestion1()
    print ("Thank you for taking the test you scored",score,"% in the end")

2 个答案:

答案 0 :(得分:0)

你打开并关闭了行中的引号:

print ("Calculate"+ str(Number1) + "+"+ str("Number2)+"")

应该是:

    print ("Calculate"+ str(Number1) + "+"+ str(Number2)+"")

这改变了整个代码中引号的使用。这只是一个明显可见的错误。首先,你需要纠正它,然后检查休息。

答案 1 :(得分:0)

你需要score = 0,否则它将不知道在第3行的上一行中添加什么 它看起来像:

import random                                                               
print ("Welcome to Math Extreme PRE EARLY PRE-ALPHA")                       
score = 0                                                                   
def AskAQuestion1():                                                        
    global score                                                            
    Number1 = random.randint(0,10)                                          
    Number2 = random.randint(0,10)                                          
    print ("Calculate"+ str(Number1) + "+"+ str(Number2)+"")                
    answer = input ()                                                       
    answer = int (answer)                                                   
    if answer == Number1+Number2:                                           
         print ("Correct")                                                  
         score = score + 1                                                  
    else:                                                                   
         print ("wrong")