兄と一緒にサッカーの試合名簿の情報を入力して編集し、チームを比較したり、選手を管理したりできるプログラムを作成しようとしています。これは、私が試した最初の「大きな」プロジェクトです。
辞書内にネストされた辞書があり、ユーザーに辞書などを作成させることができます。しかし、(raw_inputを介して)「user」に戻って編集しようとすると、行き詰まります。以下に、エラーに関連すると思われるコードの簡略版を書き留めようとしました。フルバージョンを置く必要がある場合はお知らせください。
player1 = {'stat1' : A, 'stat2' : 2, 'stat3' : 3} #existing players are the dictionaries
player2 = {'stat1' : A, 'stat2' : 2, 'stat3' : 3} # containing the name of stat and its value
position1 = {'player1' : player1} # in each position the string (name of player) is the key and
position2 = {'player2' : player2} # the similarly named dict containing the statisics is the value
position = raw_input('which position? ') # user chooses which position to edit
if position == 'position1':
print position1 # shows user what players are available to choose from in that position
player = raw_input('which player? ') #user chooses player from available at that position
if player == player1:
print player # shows user the current stats for the player they chose
edit_query = raw_input('Do you need to edit one or more of these stats? ')
editloop = 0
while editloop < 1: # while loop to allow multiple stats editing
if edit_query == 'yes':
stat_to_edit = raw_input('Which stat? (If you are done type "done") ')
if stat_to_edit == 'done': #end while loop for stat editing
editloop = editloop +1
else:
new_value = raw_input('new_value: ') #user inserts new value
# up to here everything is working.
# in the following line, player should give the name of the
# dictionary to change (either player1 or player2)
# stat_to_edit should give the key where the matching value is to be changed
# and new_value should update the stastic
# however I get TypeError 'str' object does not support item assignment
player[stat_to_edit] = new_value #update statistic
else: # end loop if no stat editing is wanted
fooedit = fooedit + 1
もちろん、「あげるべき…」などと言うときは、「あげたい…」という意味です。
要約すると、ユーザーが編集するプレーヤーを選択し、編集する統計を選択してから、新しい値を選択するようにします