2014-12-04 61 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 
相關問題