Python Forum
Homework( Solution found)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework( Solution found)
#1
Exclamation 
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
Larz60+ write Oct-10-2021, 07:32 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
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}")
Reply
#3
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}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 5 3,758 Nov-23-2020, 05:15 PM
Last Post: cananb
  Need help with this homework for a course ASAP...need solution for completion. SP04123 8 4,952 Jun-21-2020, 06:15 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020