错误的语法错误python

2024-04-26 23:43:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在努力完成我的课程,由于某些原因,我不断得到一个无效的语法。不起作用的部分是第一类不,它最初起作用,但现在已经停止了,所以我不知道是我的软件还是什么。。我的代码如下:

import random
score = 0
questions = 0
classnumber = ("1","2","3")
name= input("Enter Your Username: ")
print("Hello, " + name+(". Welcome to the Arithmetic Quiz")

classno = input("What class are you in?"))
    while group not in classnumber:
        print("Enter a valid class")
        classno=input("What class are you in?")

while questions <10:
    for i in range(10):
        number1=random.randint(1,10)
        number2=random.randint(1,10)
        op=random.choice("*-+")
        multiply=num1*num2
        subtract=num1-num2
        addition=num1+num2

        if op == "-": 
            print("Please enter your answer.")
            questions+=1
            print(" Question" ,questions, "/10")
            uinput=input(str(number1)+" - "+str(number2))
            if uinput == str(subtract):
                score+=1
                print("Correct, your score is: " ,score,)
            else:
                print ("Incorrect, the answer is: " +str(subtract))
                score+=0

        if op == "+":
            print("Please enter your answer.")
            questions+=1
            print(" Question",questions, "/10")
            uinput=input(str(number1)+" + "+str(number2))
            if uinput == str(addition):
                score+=1
                print("  Correct, your score is: ",score,)
            else:
                print("  Incorrect, the answer is: " +str(addition))
                score+=0

        if op == "*":
            print("Please enter you answer.")
            questions+=1
            print(" Question",questions, "/10")
            uinput=input(str(number1)+" * "+str(number2))
            if uinput == str(multiply):
                score+=1
                print("  Correct, your score is: " ,score,)
            else:
                print("  Incorrect, the answer is: " +str(multiply))
                score+=0

if score >9:
    print("Well done," ,name1, "your score is" ,score, "/10")
else:
 print(name1," your score is " ,score, "/10")

def no1():
     with open("class1.txt", 'a')as file:
         file.write(str(name1)+str( score)+"\n")
def no2():
     with open("class2.txt", 'a')as file:
         file.write(str(name1)+str( score)+"\n")
def no3():
     with open("class3.txt", 'a')as file:
         file.write(str(name1)+str( score)+"\n")
if group=="1":
    no1()
if group=="2":
    no2()
if group=="3":
    no3()

Tags: theanswerininputyourifisrandom
1条回答
网友
1楼 · 发布于 2024-04-26 23:43:15

这些行上有额外的括号

print("Hello, " + name+(". Welcome to the Arithmetic Quiz") # line 6 | open bracket

classno = input("What class are you in?")) # line 8 | close bracket

您还没有在任何地方定义group。我想你的意思是

while classno not in classnumber:

相关问题 更多 >