Home > OS >  Python TypeError: argument of type 'int' is not iterable problem
Python TypeError: argument of type 'int' is not iterable problem

Time:12-18

Subject of my project is "A dice is thrown 100 times. Write a program that prints the number of times each number occurs."

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)

CodePudding user response:

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.

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.

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")
  • Related