创建一个计数器,从用户那里接收一个数字,从 0 计数到该数字,告诉用户每个数字是偶数还是奇数,然后将所有数字相加并打印总和。
以下代码和注释是一个起点。每个 # 后面的文本是注释,您需要将注释替换为必要的代码:
count = 0
sum = 0
num = 0
# Ask user for a number
#All code lines that need to be inside the while loop, need to be tabbed over once.
while (count <= num):
# add count to sum
# Use the following if statement along with the remainder operator (modulus, %) to check if a number is even or odd.
if count%2!=0:
#print the number is odd
else:
#print the number is even
count = count +1 #update counter for the while loop exit condition
#print the sum
使用 for 循环
count=0
sum=0
for i in range(0,n+1):
if (i%2==0):
print("Number is Even")
else:
print("Number is Odd")
sum+=i
print(sum)
使用 While 循环:
count=0
sum=0
while(count<n):
if(count%2==0):
print("Number is Even")
else:
print("Number is odd")
count+=1
sum+=count
print(sum)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句