固定记分系统

2024-04-20 07:52:54 发布

您现在位置: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")

Tags: theanswerinputifrandomlevelpreint
2条回答

你把报价的开盘和收盘都搞砸了:

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

应该是:

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

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

您需要执行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")

相关问题 更多 >