TypeError:'int'和'str'的实例之间不支持'> ='

时间:2019-10-04 19:13:21

标签: string syntax integer python-3.7.4

我正在创建自动售货机,作为检查点测试的一部分,但是我遇到了以下反复出现的问题。

我正在使用Python 3.7.4

print("----WELCOME TO THE ABBEY GRANGE VENDING MACHINE----")
print("Please enter three coins")

first = input("Please enter your first coin: ")
first = int(first)

second = input("Please enter your second coin: ")
second = int(second)

third = input("Please enter your third coin: ")
third = int(third)

coin = int(first) + int(second) + int(third)
coin= int(coin)

if int(coin >= "15"):
    print("Thank you! You have inserted", money,"pence")
else:
    print("You have not inserted enough coins. You may not purchase anything at this time!")`
    print("Goodbye")`

代码继续出现相同的问题:

print("Or we can pick something for you but you must have over 65p")

if money > "65":
    choice = input("Would you like a generated option? ")
    if choice == "Yes":
       print (menu)

如果用户输入的第一个数字大于10,则输入10、10、10,我希望代码继续执行,但我得到:

Traceback (most recent call last):
  File "C:\Users\dell\Downloads\Abbey Grange Vending Machine.py", line 21, in <module>
    if int(coin >= "15"):
TypeError: '>=' not supported between instances of 'int' and 'str'

1 个答案:

答案 0 :(得分:0)

对于您的情况,您不能在不同数据类型intstr的两个变量之间执行逻辑运算/比较。
您也将money variable传递给print函数,但未在代码中的任何地方定义它。
以下代码可解决您的问题

print("----WELCOME TO THE ABBEY GRANGE VENDING MACHINE----")
print("Please enter three coins")

first = input("Please enter your first coin: ")
first = int(first)

second = input("Please enter your second coin: ")
second = int(second)

third = input("Please enter your third coin: ")
third = int(third)

coin = first + second + third

if (coin >= 15):
    print(f"Thank you! You have inserted, {coin} pence")
else:
    print("You have not inserted enough coins. You may not purchase anything at this time!")
    print("Goodbye")