2016-11-22 52 views
-1

我的代碼如下:初學Python的功能功能和循環

import random 

def CalcMulti(TT,TB): 
    ResultMutli = (TT * TB) 
    Answer1 = (input("What is the result of",TT,"*",TB,"?")) 
    if (ResultMulti == Answer1): 
     print("That is correct, the answer is",ResultMulti) 
    else: 
     print("That's not correct, sorry! The answer was",ResultMulti) 

def CalcDivi(TT,TB): 
    ResultDivi = (TT/TB) 
    Answer2 = int(input("What is the result of ",TT,"/",TB,"?")) 
    if (ResultDivi == Answer2): 
     print("That is correct, the answer is",ResultDivi) 
    else: 
     print("That's not correct, sorry! The answer was",ResultDivi) 

print("Thank you for using my program! Let's begin at the menu.") 
print("") 
ProgramLoop = 1 
while (ProgramLoop == 1): 
    Num1 = random.randint(1,99) 
    Num2 = random.randint(1,99) 
    print("Your two numbers are",Num1,"and",Num2,". What should we do with  these?") 
print("") 
print("1. Multiplication") 
print("") 
print("2. Division") 
print("") 
print("3. Exit Program") 
print("") 
MenuDecision = input("Enter M for Multiplication, D for Division, or E to Exit Menu!") 
ExitLoop = 1 
if (MenuDecision == "E") or (MenuDecision == "e"): 
    ProgramLoop = 0 
    print("Thank you for using my program! Bye!") 
    ExitLoop = 0 
elif (MenuDecision == "D") or (MenuDecision == "d"): 
    CalcDivi(Num1,Num2) 
elif (MenuDecision =="M") or (MenuDecision == "m"): 
    CalcMulti(Num1,Num2) 
else: 
    print("Not a valid entry, sorry! Restarting...") 
    ExitLoop = 0 
    ProgramLoop = 1 
while (ExitLoop == 1): 
    ProgramExit = input("Would you like to continue using the program? (yes/no)") 
    if (ProgramExit=="yes") or (ProgramExit=="YES") or (ProgramExit=="Yes"): 
     print("Back to the menu we go!") 
     ProgramLoop = 1 
     ExitLoop = 0 
    elif (ProgramExit=="no") or (ProgramExit=="NO") or (ProgramExit=="No"): 
     print("Thank you for using my program! Bye!") 
     ProgramLoop = 0 
     ExitLoop = 0 
    else: 
     print("Not a valid entry, yes or no please!") 
     ExitLoop = 1 

每當我運行該程序,並輸入「M」或「d」我收到以下錯誤:

Traceback (most recent call last): 
    File "C:\Users\xxxxxx\Downloads\xxxxxxxxx Assignment 4.py",  line 44, in <module> 
    CalcMulti(Num1,Num2) 
    File "C:\Users\xxxxxx\Downloads\xxxxxxxx Assignment 4.py",  line 7, in CalcMulti 
    Answer1 = (input("What is the result of",TT,"*",TB,"?")) 
TypeError: input expected at most 1 arguments, got 5 
+0

具體來說,問題是,當我打電話的代碼我MenuDecision點內我的函數,該函數無法調用,並給出了上面的錯誤。 – user7196484

回答

0

input()只需要一個字符串作爲它的輸入。目前,你正在傳遞5個字符串。你應該做的事情如下所示:

def CalcMulti(TT,TB): 
    ResultMutli = (TT * TB) 
    string = "What is the result of {}*{}?".format(TT,TB) 
    Answer1 = input(string) 
    if (ResultMulti == Answer1): 
     print("That is correct, the answer is",ResultMulti) 
    else: 
     print("That's not correct, sorry! The answer was",ResultMulti) 
1

input函數只有一個參數。正確的方法是這樣的:

input("What is the result of %s * %s?" % (TT, TB))