python-3.x - 如何将 input() 集成到迭代循环中?
问题描述
我是 python 的初学者,想尝试一下我的第一个个人项目,这是一个学生科目的分数计算器。我想运行一个 for 循环,要求用户通过 for 循环中的 input() 输入每个科目的分数。但是,它只需要一个单一的字符串参数。
scores = {}
total = 0
years = int(input("How subjects have you completed? "))
for sub in range(0, years):
scores = input("Score for subject?",x)
total += int(scores)
任何帮助都会很棒,谢谢!
解决方案
如果您想按科目跟踪分数并找到总分,那么...
subjects = dict( Math = 0, English = 0, Science = 0, History = 0 )
scores = dict()
total = 0
for sub in subjects.keys():
score = input( f"Score for {sub}? >" )
if score:
total += int( score )
scores[sub] = score
print( total )
for sub,tot in scores.items():
if tot > 0:
print( sub, tot )
推荐阅读
- php - 在 WooCommerce 中下订单后,根据订单元数据向第三方发送邮件
- apache-flink - 新版 Flink 应用部署失败
- angular - 从日期选择器输入中跳出,选择的日期更改为什么?
- arrays - C中的选择排序算法不会打印整个排序数组
- javascript - 在 vue js 中的 reader.Onload() 之后执行另一个函数
- ios - 尝试使用 Alamofire (swift) 请求 POST 时,Windows 服务器返回 404
- vue.js - VueJS - v-用于从道具嵌套对象中获取未定义的值
- javascript - Axios GET 请求返回 [object Promise]
- reactjs - 使用 useContext 时错误对象不可迭代(无法读取属性 Symbol(Symbol.iterator))
- javascript - 我无法解密我的文本,但我可以加密有人可以检查我的代码吗