简体   繁体   English

Python TypeError:“int”类型的参数不是可迭代的问题

[英]Python TypeError: argument of type 'int' is not iterable problem

Subject of my project is "A dice is thrown 100 times. Write a program that prints the number of times each number occurs."我的项目的主题是“掷骰子 100 次。编写一个程序,打印每个数字出现的次数。”

I made it but when i run the code this error occurs:我做到了,但是当我运行代码时会发生此错误:

Traceback (most recent call last):
  File "c:\Users\pc\Desktop\zar atma.py", line 15, in <module>
    if (1 in sayilar):
TypeError: argument of type 'int' is not iterable

Here is my code:这是我的代码:

import random

sayilar = ( )
sayi1 = ( )
sayi2 = ( )
sayi3 = ( )
sayi4 = ( )
sayi5 = ( )
sayi6 = ( )
  
for sayi in range(100):
  sayilar = random.randint(1,6)
  print(sayilar)

if (1 in sayilar):
     sayi1.append(1 in sayilar)
     print(sayi1)
elif (2 in sayilar):
    sayi2.append(2 in sayilar)
elif (3 in sayilar):
    sayi3.append(3 in sayilar)
elif (4 in sayilar):
    sayi4.append(4 in sayilar)
elif (5 in sayilar):
    sayi5.append(5 in sayilar)
elif (6 in sayilar):
    sayi6.append(6 in sayilar)

print("---toplam 1 sayısı---")
len(sayi1)
print("---toplam 2 sayısı---")
len(sayi2)
print("---toplam 3 sayısı---")
len(sayi3)
print("---toplam 4 sayısı---")
len(sayi4)
print("---toplam 5 sayısı---")
len(sayi5)
print("---toplam 6 sayısı---")
len(sayi6)

below is a sample code that solves your problem.下面是解决您的问题的示例代码。

As already pointed out in the comments of your question, the first error you run into is a type error, because you are passing an integer where an iterable is expected.正如您在问题的评论中已经指出的那样,您遇到的第一个错误是类型错误,因为您正在传递一个 integer ,其中需要一个可迭代的。

Secondly, in such a case you should use list and not tuples, as tuples are immutable and can not be changed after creation.其次,在这种情况下,您应该使用列表而不是元组,因为元组是不可变的,并且在创建后无法更改。 Lists are much more flexible and better for your purpose.列表更灵活,更适合您的目的。

Another hint, if you write code, stick to english as it will make your code understandable for everyone.另一个提示,如果您编写代码,请坚持使用英语,因为这将使您的代码对每个人都可以理解。 This is especially important if you share your code with others such as on stackoverflow.如果您与其他人(例如在 stackoverflow 上)共享您的代码,这一点尤其重要。

import random

outcomes = []

for i in range(0, 100):
    outcomes.append(random.randint(1,6))

for i in range(1,7):
    print(f"The number {i} occured {outcomes.count(i)} times")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM