每次在輸入欄位中輸入“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)
uj5u.com熱心網友回復:
試試這個,它應該有幫助。
代碼每次更新后都會計算總數。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438551.html
標籤:Python 字典 python-3.6
上一篇:按索引更改值字典
