简体   繁体   中英

Python Beginning: Can't figure out how to tell user their number is odd or even (for 5 numbers)

I just signed up and I'm not sure if this will go to the right forum (assuming that's a thing here).

I just started learning python a couple of weeks ago. We're doing iteration.

This is our reading material for this assignment: https://www.py4e.com/html3/05-iterations

This is the assignment along with my code.

Write a program to allow the user to enter five numbers, one at a time. After each entry, tell the user whether the number is odd or even. At the end of all the entries, display the sum total of all the entered numbers on the screen.

x = 0
num = 0   
while x < 5:
    x += 1
    num += int(input("Enter a number: "))
    mod = num % 2
    if mod > 0:
        print(num-x,"is an odd number.")
    else:
        print(num-x,"is an even number.")
print("Your total is",num)

This doesn't work for the odd and even portion of the assignment. I'm pretty sure it has to do with 'num' variable changing every time the user inputs a new number and, instead of just telling the user the number they just entered was an even or an odd number, it's adding the numbers.

So, if first user input is 3, it will say it's odd. But if they input 3 again for the second number, it'll say it's Even because it's adding 3 + 3 to get 6. Which, obviously, I don't want it to total up the numbers until the final print.

This is my output:
Enter a number: 1
0 is an odd number.
Enter a number: 1
0 is an even number.
Enter a number: 1
0 is an odd number.
Enter a number: 1
0 is an even number.
Enter a number: 1
0 is an odd number.
Your total is 5

Obviously, all those 1s should be odd and I just now realized that 0 doesn't belong there.

You are trying to use num for two different purposes:

  • the cumulative sum
  • the number that has just been entered

and as a result you end up testing the odd/evenness of the cumulative sum rather than the number that has just been entered.

Separate them out into two different variables num and total and then it will become easier.

I suggest also using a for instead of while loop for x :

total = 0   
for x in range(5):
    num = int(input("Enter a number: "))
    mod = num % 2
    if mod > 0:
        print(num, "is an odd number.")
    else:
        print(num, "is an even number.")
    total += num

print("Your total is", total)
x = 0
total_num = 0
while x < 5:
    num = int(input("Enter a number: "))
    mod = num % 2
    if mod > 0:
        print(num,"is an odd number.")
    else:
        print(num,"is an even number.")
    total_num += num
    x += 1

print("Your total is",total_num)

You are partially right. The logic regarding checking if the number is odd/even is good. Now, the thing is that each time you are reading a number, you are adding that new number to the previous one. The solution here is to have another variable to keep track of the total, that way you are checking individually if the number is odd/even and also getting the total sum at the end.
Also, it looks cleaner if you check mod == 0 instead of mod > 0 . So just switch those. Finally, you don't need to substract x from your num , x is just your counter to keep track of which iteration you are at a given moment.

x = 0
num = 0
total = 0
while x < 5:
    x += 1
    num = int(input("Enter a number: ")) # Read new number
    total += num # Add new number to the total
    mod = num % 2 # Check if new number is odd
    if mod == 0:
        print(num,"is an even number.")
    else:
        print(num,"is an odd number.")
print("Your total is",total)

Instead of adding up the variable num assign a different variable to count the numbers entered.

x = 0
num = 0
sum1 = 0
while x < 5:
        x += 1
        num = int(input("Enter a number: "))
        sum1 += num
        mod = num % 2
        if mod > 0:
               print(num,"is an odd number.")
        else:
               print(num,"is an even number.")
print("Your total is",sum1)

Have made slight changes in the indentation, assigned an extra variable and it worked.

I'm not a Python programmer but you'll need a third variable to calculate the total. At the moment num is doing that part of the job, when using += to assign a value. Which means it's doing the mod on the subtotal and not on the entry value.

It should be:

num = int(input("Enter a number: "))

That's without +.

And then you need a third variable to show the total at the very end:

total = total + num

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM