首页 > 解决方案 > Python:出现错误:TypeError:“int”类型的参数不可迭代

问题描述

from sys import exit
def gold_room():
    print ("how much gold you need)")
    amount = int(input("amount:"))
    if 0 in amount or  1 in amount:
        print(amount)
gold_room()

输出

how much gold you need)
amount:1
 line 8, in gold_room
    if 0 in amount or  1 in amount:
TypeError: argument of type 'int' is not iterable

Process finished with exit code 1

为什么我会收到此错误?

标签: python

解决方案


正如其他人提到的,目前尚不清楚您要做什么。如果要打印amount等于1还是0,可以用这个

from sys import exit
def gold_room():
    print ("how much gold you need)")
    amount = int(input("amount:"))
    if 0 == amount or  1 == amount:
        print(amount)
gold_room()

或者如果数字中有 1 或 0(即 123、220),您想打印金额,您可以使用此

from sys import exit
def gold_room():
    print ("how much gold you need)")
    amount = input("amount:")
    if '0' in amount or  '1' in amount:
        print(amount)
gold_room()

推荐阅读