python - How to calculate the total of all the number between 1 and the number that the user entered?
问题描述
I have to ask the user to enter an integer number and the program will calculate the total of all the number between 1 and the number that the user entered. For example, if I enter 5
and the output will be The total is 15
解决方案
接受用户输入。分配total = 0然后循环
num = int(input("Enter number: "))
tot = 0
for i in range(num + 1):
tot += i
print(tot)
推荐阅读
- spring-boot - 带有 oauth2 的 Spring Security 5 导致“principalName 不能为空”错误
- c# - 我想每 3 次尝试使脚本处于活动状态或不活动状态
- python - 如何使用python将2列相似数据连接到同一个excel文件中具有相同列标题的单列中
- android - 如何使用 volley 在 Kotlin 中同步 mysql 和 sqlite 数据库
- r - 比较三个分类列并用 0,1 或 2 表示结果
- r - 在函数 (R) 中将列内容设置为变量
- amazon-web-services - AWS Fargate 无法通过负载均衡器/公共 IP 访问 dockerized spring boot 应用程序
- php - 由于保护功能,调用方法尝试不起作用
- javascript - 如何在 .js 文件中使用 .ts 文件中的变量
- arrays - 尝试在 Flutter Streambuilder 中构建一个喜欢/不喜欢按钮 - 不工作