[英]I am new to python and i can't find why this program is not working?
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
I can't understand why it's not working i know something is wrong with the while loop but i can't find it.我不明白为什么它不起作用我知道 while 循环有问题但我找不到它。 Note: I"ve just started learning python !注意:我刚刚开始学习 python !
input()
returns a string, eg. input()
返回一个字符串,例如。 "1"
. "1"
。 you need to cast it to an integer for your comparison to work.您需要将其转换为整数才能进行比较。
clicky = input(...)
clicky = int(clicky)
Here is the correct Answer:以下是正确答案:
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
The input on the variable clicky requires a cast, as you did correctly later for the variables a and b.变量 clicky 上的输入需要强制转换,正如您稍后对变量 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
Next time, when you open a ticket please try to make some effort to clarify what it is not working, what it is the error or the unexpected behavior you get.下次,当您打开故障单时,请尝试阐明它不工作的原因,是什么错误或您遇到的意外行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.