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

Why is dictionary not printing updated value Python 3.6

每次在输入字段中输入 'g' 时,字典 'goal-totals' 应该更新并且新值反映在打印的 'totals' 输出中。除了第一次输入条目 'g' 之外,该代码都有效。如何使第一个条目反映在打印的 'totals' 输出中?

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)

试试这个,应该会有帮助。

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

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)