初学者Python函数函数和循环

时间:2016-11-22 20:00:48

标签: python function

我的代码如下:

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

2 个答案:

答案 0 :(得分:1)

input函数只接受一个参数。正确的方法是这样的:

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

答案 1 :(得分: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)
相关问题