TypeError: '> =' не поддерживается между экземплярами int и str - PullRequest
1 голос
/ 04 октября 2019

Я создаю торговый автомат как часть теста контрольной точки, но сталкиваюсь с повторяющейся проблемой, указанной ниже.

Я использую 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 для первого числа, поскольку оно больше 15, я ожидаю, что код будет продолжен, но вместо этого я получу:

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 голосов
/ 04 октября 2019

В вашем коде много ошибок, кроме вашей ошибки. Во-первых, вы не можете выполнить логическую операцию / сравнение между двумя переменными разных типов данных int и str для вашего случая. Вы также передаете функцию 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")
...