谁能帮助如何在 Python 上编写此代码?

亚历克斯

创建一个计数器,从用户那里接收一个数字,从 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python代码中编写此算法?

Python:如何以“可读”方式编写此python单行代码?

如何使用Python在OpenCV中编写此代码?

如何在没有一行 for 循环的情况下编写此 Python 代码?

如何在 Flutter 应用程序中编写此 Python 代码?

如何在python中编写此公式?

如何使此代码在python上运行得更快?

如何快速编写此代码?

谁能告诉我如何在Python上提取并显示图像中的文本

如何重新编写此python代码以在pyinstaller生成的exe中工作?

如何在Windows上帮助Tox找到正确版本的python?

如何在python代码段上应用并行或异步I / O文件编写

编写此Python代码的更简洁的方法

如何在python中改进此代码?

如何在python中优化此代码

如何为iMacros编写此代码的循环?

如何使用嵌套的for循环编写此代码?

如何以简单的方式编写此代码?

谁能帮助我解决此SQLite 3 Python操作错误?

在Python上编写代码或编写代码是否存在问题?

如何优化此Python代码?

如何优化此Python代码?

如何在Python中编写此方程式?

如何在python / selenium / scraping中编写此if语句

在Windows 10上按“ Ctrl + C”按钮时,Python代码显示在屏幕上。如何隐藏此代码?

谁能帮助我理解此C ++代码?

谁能帮助我理解此代码?Tensorflow Lite

如何编写一个 Python 代码来在 0.0.0.0 上提供 .html 文件?

TS测试!如何在 TypeScript 中编写此 JS 代码?