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

问题描述 投票:1回答:1

[作为检查点测试的一部分,我正在创建自动售货机,但是我面临下面所述的重复出现的问题。

我正在使用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'
string syntax integer python-3.7.4
1个回答
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")
© www.soinside.com 2019 - 2024. All rights reserved.