为什么字典不打印更新后的值 Python 3.6
每次在输入字段中输入“g”时,字典“goal-totals”都应该更新,并且新值反映在打印的“totals”输出中。除了第一次输入条目“g”外,该代码均有效。如何使第一个条目反映在打印的“总计”输出中?
from IPython.display import clear_output
goal_totals = {'goals' :0}
active = True
while active:
totals = f'Goals: {goal_totals["goals"]}'
command = input()
if command == 'g':
goal_totals["goals"] += 1
clear_output()
print(totals)
Each time 'g' is entered into the input field, the dictionary 'goal-totals' should be updated and the new value reflected in the printed 'totals' output. The code works all but for the first time an entry 'g' is entered. How do I make the first entry reflect in the printed 'totals' output?
from IPython.display import clear_output
goal_totals = {'goals' :0}
active = True
while active:
totals = f'Goals: {goal_totals["goals"]}'
command = input()
if command == 'g':
goal_totals["goals"] += 1
clear_output()
print(totals)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个,应该有帮助。
代码每次更新后都会计算总数。
Try this, it should help.
The code calculates the totals each time after its updated.