2014-12-04 71 views
-3
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 

与最后部分的主要问题是显示不正确的操作数类型,但这只在加法和乘法不是减法这是混淆了每次发生的程序要求一加乘法和我回答正确或不正确它显示+ =:intNoneType以下消息不受支持的操作数类型。我的分数和功能不程序一旦运行工作

+0

请把这个'while a == True:'摆脱掉。当条件满足时,只需使用'while True:'和'break'。 – Matthias 2014-12-04 15:21:09

回答

3

您没有在您的函数中正确缩进return score。目前,它返回的分数只有在其他部分:

if question2 == total_m: 
    print("Correct!") 
    score=score+1 
else: 
    print("Incorrect!") 
    print(total_m) 
    return score 

所以,如果打印正确,该函数返回“无”,即NoneType
改为

if question2 == total_m: 
    print("Correct!") 
    score=score+1 
else: 
    print("Incorrect!") 
    print(total_m) 
return score