Home > Back-end >  Why is dictionary not printing updated value Python 3.6
Why is dictionary not printing updated value Python 3.6

Time:03-07

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)

CodePudding user response:

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)
  • Related