Python program to count the number of times the user enters the integer ‘2’ (using while loop and for loop)

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
callout-flag

Love this read? Subscribe to Modernage!

Do you have any queries? You can ask in the comments. We, at Modernage answer each and every question, asked in the comments, so If you have any queries, or you want to know about any specific platform you can ask in the comments below

Author

This Post Has 4 Comments

  1. Faisal

    It’s Working.
    Thank you

  2. Amy

    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.

  3. Sugar Defender

    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.

Leave a Reply

girl-carrying-document

More Articles.

girl-carrying-document

More Articles.