def change():
if choice == 1: #<--- issue here
while True:
amt = float(input("Enter amount of cash: $"))
if amt >= a:
print("Your change will be: ${:.2f}".format(amt - a))
break
else:
print("Not enough, missing: ${:.2f}".format(a - amt))
input("press enter to continue")
a = 15.60
b = 56.12
c = 89.53
d = 32.93
print("1. a: $15.60\t\t\t2. b: $56.12\n3. c: $89.53\t\t\t4. d: $32.93")
choice = input("Choose product (1-4): ")
change()
如果我删除第2行,它将正常运行,但不会选择选项1。我希望它可以在选择选项1时运行。由于某种原因,它不允许我在while循环之前放置if语句。有解决办法吗?
答案 0 :(得分:2)
这是您输入语句中的问题。在python 3中,输入默认为字符串。因此,您需要将其转换为整数,如下所示。
NSBindingName(rawValue: )
答案 1 :(得分:1)
在python 3.x中将字符串转换为整数使用int()
choice = int(input("Choose product (1-4): "))
答案 2 :(得分:0)
请尝试以下代码
def change():
print(choice, choice == 1, type(choice), int(choice) == 1)
if int(choice) == 1: #<--- issue here
while True:
amt = float(input("Enter amount of cash: $"))
if amt >= a:
print("Your change will be: ${:.2f}".format(amt - a))
break
else:
print("Not enough, missing: ${:.2f}".format(a - amt))
input("press enter to continue")
a = 15.60
b = 56.12
c = 89.53
d = 32.93
print("1. a: $15.60\t\t\t2. b: $56.12\n3. c: $89.53\t\t\t4. d: $32.93")
choice = input("Choose product (1-4): ")
change()