【问题标题】:'int' object is not iterable in strings'int' 对象在字符串中不可迭代
【发布时间】:2022-01-13 22:59:26
【问题描述】:

所以我试图获取数字 1、2、3... 的列表以在控制台上单独打印出来,但它总是说 int 对象不可迭代。 我的代码:

def run_seed_code(told_seed):
    for letter in told_seed:
        print(letter)

run_seed_code(1234321)

【问题讨论】:

    标签: python-3.x string iterable


    【解决方案1】:

    尝试将数字转换为字符串。

    for char in "Hello World":
        print(char)
    

    这会将每个字母打印在一个新行上,因为它会遍历 字符串

    整数是不可迭代的,你不能循环遍历它。

    在函数中添加一个字符串施法者或者只是将参数变成一个字符串都可以正常工作。

    def run_seed_code(told_seed):
        for letter in str(told_seed): # Convert to a string
            print(letter)
    
    run_seed_code("1234321") # Or pass a string into the argument instead.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-02
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 2019-07-24
      • 2017-09-11
      相关资源
      最近更新 更多