Python 入门:无法弄清楚如何告诉用户他们的数字是奇数还是偶数

这是作业和我的代码。

编写一个程序,允许用户输入五个数字,一次一个。每次输入后,告诉用户该数字是奇数还是偶数。在所有条目的末尾,在屏幕上显示所有输入数字的总和。

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)

这不适用于作业的奇数和偶数部分。我很确定这与每次用户输入新数字时&ldquo;num&rdquo;变量的变化有关,并且它不只是告诉用户他们刚刚输入的数字是偶数还是奇数,而是将数字相加。


因此,如果第一个用户输入是 3,它会说它是奇数。但是,如果他们再次输入 3 作为第二个数字,它会说它是 Even,因为它添加 3 + 3 得到 6。显然,我不希望它在最终打印之前将数字加起来。


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

显然,所有这些 1 都应该是奇数,而我现在才意识到 0 不属于那里。


慕雪6442864
浏览 118回答 5
5回答

月关宝盒

您尝试用于num两个不同的目的:累计总和刚刚输入的号码结果,您最终测试的是累积和的奇数/偶数,而不是刚刚输入的数字。将它们分成两个不同的变量num,total然后就会变得更容易。我建议还使用forfor代替while循环x:total = 0&nbsp; &nbsp;for x in range(5):&nbsp; &nbsp; num = int(input("Enter a number: "))&nbsp; &nbsp; mod = num % 2&nbsp; &nbsp; if mod > 0:&nbsp; &nbsp; &nbsp; &nbsp; print(num, "is an odd number.")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(num, "is an even number.")&nbsp; &nbsp; total += numprint("Your total is", total)

ABOUTYOU

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

慕的地8271018

我不是 Python 程序员,但您需要第三个变量来计算总数。目前,当使用 += 赋值时, num 正在执行这部分工作。这意味着它是对小计而不是条目值进行取模。它应该是:num&nbsp;=&nbsp;int(input("Enter&nbsp;a&nbsp;number:&nbsp;"))那是没有+的。然后你需要第三个变量来显示最后的总数:total&nbsp;=&nbsp;total&nbsp;+&nbsp;num

宝慕林4294392

num分配一个不同的变量来计算输入的数字,而不是将变量相加。x = 0num = 0sum1 = 0while x < 5:&nbsp; &nbsp; &nbsp; &nbsp; x += 1&nbsp; &nbsp; &nbsp; &nbsp; num = int(input("Enter a number: "))&nbsp; &nbsp; &nbsp; &nbsp; sum1 += num&nbsp; &nbsp; &nbsp; &nbsp; mod = num % 2&nbsp; &nbsp; &nbsp; &nbsp; if mod > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(num,"is an odd number.")&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(num,"is an even number.")print("Your total is",sum1)对缩进进行了细微的更改,分配了一个额外的变量并且它起作用了。

尚方宝剑之说

你说对了一部分。检查数字是否为奇数/偶数的逻辑很好。现在的问题是,每次你读取一个数字时,你都会将这个新数字添加到前一个数字上。这里的解决方案是使用另一个变量来跟踪总数,这样您就可以单独检查数字是否为奇数/偶数,并在最后得到总和。另外,如果你检查mod == 0而不是检查,它看起来会更干净mod > 0。所以只需切换这些即可。最后,您不需要x从您的 中减去num,x只是您的计数器来跟踪您在给定时刻的迭代。x = 0num = 0total = 0while x < 5:&nbsp; &nbsp; x += 1&nbsp; &nbsp; num = int(input("Enter a number: ")) # Read new number&nbsp; &nbsp; total += num # Add new number to the total&nbsp; &nbsp; mod = num % 2 # Check if new number is odd&nbsp; &nbsp; if mod == 0:&nbsp; &nbsp; &nbsp; &nbsp; print(num,"is an even number.")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(num,"is an odd number.")print("Your total is",total)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python