这个代码怎么了?它不工作!每一次其他情况都在起作用

2024-06-16 14:32:32 发布

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

def keti():
    if payf=="y":
        print("The fees are {}".format(amount))
        if payf == "n":
            amount = amount +10
            print ("the fees are {}".format (amount ))

def keti2():
    if payf=="y":
        amount =2*amount
        print("The fees are {}".format(amount))
        if payf == "n":
            amount =2*amount
            amount = amount +10
            print ("the fees are {}".format (amount ))

ket=int(input("no of kts"))
print(ket)
payf = str(input("hav u paid"))
print (payf)
amount=250
if (ket=="1"):
    keti()
elif (ket=="2"):
    keti2 ()
else:
    print ("wrong input")

这里面怎么了? 每次执行else语句时 我不明白这里面出了什么问题

这里面怎么了? 每次执行else语句时 我不明白这里面出了什么问题


Tags: theformatinputifdefamountelseare
2条回答
ket=int(input("no of kts"))     # you have converted ket into int object 
print(ket)
if (ket=="1"):                   # here your trying to compare an int object and string object ,try it with ket==1 or ket==int("1")
    keti()
elif (ket=="2"):
    keti2 ()
else:
    print ("wrong input")

如果要比较int对象和string,请尝试如果ket==1:

您已经将第一个输入转换为int,在这里:

ket = int(input("no of kts"))

因为它是整数,正确的比较应该是

if (ket == 1):
  ...
elif (ket == 2):
  ...

相关问题 更多 >