Here is the code for the python program to count the number of times the user enters the integer ‘2’ (using while loop and for loop). First, the problem is solved is using for loop and then while loop.
# Implement a program that prompts the user to enter 10 integers (there can be repetition). The
# program will count the number of times the user enters the integer ‘2’. The count will be
# displayed on the output screen. Implement using both while and for loops
# Using while loop
user_input_count = 1
two_count = 0
while user_input_count <= 10:
user_input = int(input('Enter the number: '))
if user_input == 2:
two_count +=1
user_input_count += 1
print("You entered the integer '2',", two_count, "times.")
#Usind for loop
user_input_count = 1
two_count = 0
for i in range(10):
user_input = int(input('Enter the number: '))
if user_input == 2:
two_count += 1
print("You entered the integer '2',", two_count, "times.")user_input_count = 1
two_count = 0
# This code is contributed by Ahmad Shafiq
This Post Has 4 Comments
It’s Working.
Thank you
Glad it helped!
Hi,
I need a program that does the following:
20 students select their month of birth as an integer (1-12). Students with months 1-4 get assigned to group X, from 5-8 assigned to group Y and from 9-12 to group Z. It must also count how many students are in each group after all 20 register. Please help.
Hello i think that i saw you visited my weblog so i came to Return the favore Im trying to find things to improve my web siteI suppose its ok to use some of your ideas.