a=True
import random #Here i imported a random module
score=0 #I made a variable called score and set it to 0
while a == True: #Opens a loop called a
name=input("What is your name? ") #I asked the user their name here if name == (""):
if name.isdigit() or name == (""): #If the user enters a number or nothing something will happen
print("Incorrect Name") #This is the message that appears when the the name is a number or is nothing is incorrect
else:
a=False #Here i closed a loop
print("Welcome To my quiz " +str(name)) #This is the welcoming message
def addition(): #Here i define a function called addition
score=0 #Here the score shows score
first_number_a=random.randint(1,10) #The program gets a random number
second_number_a=random.randint(1,10) #The program gets another random number
question1=int(input("What is " +str(first_number_a)+ "+" +str(second_number_a)+ " ")) #Here the program asks the user a question
total_a=(first_number_a+second_number_a) #The answer to the question above
if question1 == total_a: #If answer is equal to the new variable c which is the answer to the question
print("Correct!") #This tells the user they got it correct
score=score+1 #This adds a point to the score
else: # if answer is not the variable
print("Incorrect!") #Here the program will print that the user is incorrect
print(total_a) #Here the program prints the correct answer if they got the question wrong
return score #This keeps the score safe
def multiplication():
score=0
first_number_m=random.randint(1,10)
second_number_m=random.randint(1,10)
question2=int(input("What is " +str(first_number_m)+ "*" +str(second_number_m)+ " "))
total_m=(first_number_m*second_number_m)
if question2 == total_m:
print("Correct!")
score=score+1
else:
print("Incorrect!")
print(total_m)
return score
def subtraction():
score=0
first_number_s=random.randint(1,10)
second_number_s=random.randint(1,10)
question3=int(input("What is " +str(first_number_s)+ "-" +str(second_number_s)+ " "))
total_s=(first_number_s-second_number_s)
if question3 == total_s:
print("Correct!")
score=score+1
else:
print("Incorrect!")
print(total_s)
return score
qw=["a" , "b" , "c"] #List Of Letters that will be randomly selected to then start a function
for i in range(0,10): #Thsi willrepeat the process listed below however many times in this example 10 times
random_Letter=random.choice(qw) #Selets a random letter
if random_Letter == "a": #If the random letter is a
score += addition() #It will use the function addition
if random_Letter == "b":
score += multiplication()
if random_Letter == "c":
score += subtraction()
print("your score is " +str(score)+ " Out of 10") #Tells the user their final score
程序运行它的最后一部分的主要问题是显示错误的操作数类型,但这只发生在加法和乘法而不是减法时,每次程序要求添加乘法时都会引起混淆,我回答正确或不正确为+ =:int
和NoneType
显示以下消息不支持的操作数类型。
答案 0 :(得分:3)
您没有正确缩进功能中的return score
。目前,它仅在else部分返回分数:
if question2 == total_m:
print("Correct!")
score=score+1
else:
print("Incorrect!")
print(total_m)
return score
因此,如果您打印正确,该函数将返回“nothing”,即NoneType
改为
if question2 == total_m:
print("Correct!")
score=score+1
else:
print("Incorrect!")
print(total_m)
return score