我是python新手,我不知道为什么这个程序不工作?

2024-09-25 00:26:22 发布

您现在位置:Python中文网/ 问答频道 /正文

def snt(a1, b1): 
     snt0 = a1+b1
     return snt0
def mnt(a1, b1): 
     mnt0 = a1*b1
     return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i=1
while (i>0):
    clicky=input("For sum click on 1 for multiplcation click on 2: ")
    if clicky==1:
        a=int(input("enter a"))
        b=int(input("enter b"))
        f=snt(a,b)   
        print(a ,"+", b ,"=",f)
        input("Click to go out")
        break
   elif clicky==2:
        a=int(input("enter a"))
        b=int(input("enter b"))
        f=mnt(a,b) before
        print(a ,"*", b ,"=", f)
        input("Click to go out")
        break
   else:
        i += 1

我不明白为什么它不工作,我知道有些东西是错误的,而循环,但我找不到它。 注意:我刚刚开始学习python


Tags: toinputreturndefa1b1intsum
3条回答

input()返回字符串,例如"1"。您需要将其转换为整数,以便比较工作

clicky = input(...)
clicky = int(clicky)

变量clicky的输入需要一个cast,正如您稍后对变量a和b所做的那样

def snt(a1, b1):
    snt0 = a1 + b1
    return snt0

def mnt(a1, b1):
    mnt0 = a1 * b1
    return mnt0


print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i = 1
while (i > 0):
    clicky = int(input("For sum click on 1 for multiplcation click on 2: "))
    if clicky == 1:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = snt(a, b)
        print(a, "+", b, "=", f)
        input("Click to go out")
        break
    elif clicky == 2:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = mnt(a, b)
        print(a, "*", b, "=", f)
        input("Click to go out")
        break
    else:
        i += 1

下一次,当你打开一张罚单时,请试着弄清楚什么是不起作用的,什么是错误或者你得到的意外行为

以下是正确答案:

def snt(a1, b1):
     snt0 = a1+b1
     return snt0
def mnt(a1, b1): 
     mnt0 = a1*b1
     return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i=1
while (i > 0):
   clicky = input("For sum click on 1 for multiplcation click on 2: ")
   clicky = int(clicky)
   if clicky == 1:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = snt(a,b)   
        print(a ,"+", b ,"=",f)
        input("Click to go out")
        break
   elif clicky == 2:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = mnt(a,b)
        print(a ,"+", b ,"=", f)
        input("Click to go out")
        break
   else:
        i += 1

相关问题 更多 >