为什么字典不打印更新后的值 Python 3.6

发布于 2025-01-12 02:23:13 字数 404 浏览 0 评论 0原文

每次在输入字段中输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2025-01-19 02:23:13

试试这个,应该有帮助。

代码每次更新后都会计算总数。

from IPython.display import clear_output
     
goal_totals = {'goals' :0}              
active = True
while active:
    command = input()
    if command == 'g':
        goal_totals["goals"] += 1
    totals = f'Goals: {goal_totals["goals"]}'
    clear_output()
    print(totals)

Try this, it should help.

The code calculates the totals each time after its updated.

from IPython.display import clear_output
     
goal_totals = {'goals' :0}              
active = True
while active:
    command = input()
    if command == 'g':
        goal_totals["goals"] += 1
    totals = f'Goals: {goal_totals["goals"]}'
    clear_output()
    print(totals)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文