Python Forum
Homework( Solution found) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Homework( Solution found) (/thread-35223.html)



Homework( Solution found) - Zaochen - Oct-10-2021

hello just starded to following studies in python but there are short(4h once in 2 weeks) and dont explain everything what we get in in our homework
so i have a question on how to find a specific number in a loop and print it how many time it was in that loop i did tried with .count() but i do get an error that its not possible with int AttributeError: 'int' object has no attribute 'count'

i did try and other options but really cant find this one
and im already sitting for few hours on this one
for x in range(1,5):
    num=int(input('number: '))
    print(num.count(0))
my home work is to find 0 where user put in numbers
Each of the following n lines contains one integer.
Count and print the number of 0
like
Output:
#input 10 0 30 20 0 30 50 #output 'number of 0's is 2'
all the help is appreciated


RE: Homework( Solution found) - Daring_T - Dec-23-2021

I am not exactly sure on the matter of input? Is it only 7 numbers? Here are two different versions of what I think they're asking for.

# This version finds how many 0's out each iteration of the loop
zeros = 0
for x in range(1, 7):
	if int(input('number: ')) == 0:
		zeros = zeros + 1
	print(f"number of 0's is {zeros}")
# This version asks for the 7 numbers first then counts the amount 0's after
nums = []
for x in range(1, 7):
	num = int(input('number: '))
	print(num)
	nums.append(num)

# nums = [10, 0, 30, 20, 0, 30, 50]
zeros = nums.count(0)
print(f"number of 0's is {zeros}")



RE: Homework( Solution found) - BashBedlam - Dec-23-2021

It would me much easier if you left the input numbers as strings instead of changing them to intergers.
# This version asks for the 7 numbers first then counts the amount 0's after
nums = '' 
for x in range(7):
    num = input('number: ')
    nums = nums + num
 
zeros = nums.count('0')
print(f"number of 0's is {zeros}")