Write a program that will ask a user for how many numbers they would like to check. Then, using a for loop, prompt the user for a number, and output if that number is even or odd.


Benchmark:

Prompt the user to answer the question, "How many numbers do you need to check?"

Create and initialize two count variables - one for odd and one for even.

Based on the previous input, create a for loop that will run that exact number of times.

Prompt the user to "Enter number: "

If that number is even, output "[number] is an even number."

Update the even count variable.

Or else if the number is odd, output "[number] is an odd number."

Update the odd count variable.

Output "You entered [number] even number(s)."

Output "You entered [number] odd number(s)."

Please write the code in the Python language.

Respuesta :

Answer:

num = int(input("How many numbers do you need to check? "))

even = 0

odd = 0

i = 1

while i <= num:

    userinput = int(input("Enter number: "))

    if userinput%2 == 0:

         print(str(userinput)+" is an even number")

         even = even + 1

    else:

         print(str(userinput)+" is an odd number")

         odd = odd + 1

    i = i + 1

       

print("You entered "+str(even)+" even number(s).")

print("You entered "+str(odd)+" even number(s).")

Explanation:

This line prompts user for the number of numbers to check

num = int(input("How many numbers do you need to check? "))

The next two lines initialize even and odd count to 0

even = 0

odd = 0

This line initialize the while loop counter to 1

i = 1

while i <= num:

This line prompts user for input

    userinput = int(input("Enter number: "))

The following if and else statements check for even and odd numbers, respectively  

  if userinput%2 == 0:

         print(str(userinput)+" is an even number")

         even = even + 1

    else:

         print(str(userinput)+" is an odd number")

         odd = odd + 1

    i = i + 1

       

The next two lines print number of even and odd numbers respectively

print("You entered "+str(even)+" even number(s).")

print("You entered "+str(odd)+" even number(s).")

Answer:

c = int(input("How many numbers do you need to check? "))

o = 0

e = 0

for i in range (c):

   i = int(input("Enter number: "))

   r = i % 2

   if (r == 1):

       print (str(i) + " is an odd number.")

       o=o+1

   if (r==0):

       print(str(i) + " is an even number.")

       e=e+1

print("You entered " + str(e) + " even number(s).")

print("You entered " + str(o) + " odd number(s).")

   

Explanation:

ACCESS MORE