문제를 해결하는 첫 번째 단계는 정확히를 정의하는 것입니다. 승리/손실 추적기를 정확히 사용하려는 의도의 예는 자신이하려는 일을 정확하게 설명하는 데 도움이되었을 것입니다.
당신은 가장 가능성/필요가 없습니다 매장 승리에 사용자 입력에 의한 변수 이름을 정의합니다. 관련 정보의 각 그룹 (예 : 한 사용자의 레코드)에 대한 사전을 작성하십시오. 이 사전은 아마 당신은 아래에 무엇을 찾고 있는지 등 내가 근사했습니다
사용자의 이름, 자신의 승리 & 손실 계수를 저장합니다.
#create list
gameTracker=[]
#method to get string input from user. Extremely basic solely to answer question. If you actually intend to get user input you'll need proper type checking in these functions
def inputString(Message):
return raw_input(Message)
def inputInt(Message):
return int(raw_input(Message))
#Replace "InputString('')" with whatever function is returning user input
#Loop twice & input records for 2 players in gameTracker list. Each player's records is stored in a dictionary
for i in range(2):
gameTracker.append(
{"playerName":inputString("Input player name: "), "playerMethod":inputString("Input method: "),"gamePlayed":inputString("Input game: "),"wins":inputInt("Input wins: "),"losses":inputInt("Input losses: ")}
)
#Print values for each player (dictionary), from gameTracker list
for i in range(len(gameTracker)):
print(gameTracker[i])
#Update the win count for player named 'peter'
for i in range(len(gameTracker)):
if gameTracker[i]['playerName']=="peter":
#get new win count- replace with whatever method does this for your application
newWinCount=inputInt("Enter new win count: ")
gameTracker[i]['wins']=newWinCount
#Add one win to record for player 'peter'
gameTracker[i]['wins']+=1
print(gameTracker[i])
예 출력 :
Records for 2 players stored in "gameTracker"
{'playerName': 'peter', 'wins': 4, 'losses': 5, 'playerMethod': 'character', 'gamePlayed': 'GTA'}
{'playerName': 'david', 'wins': 5, 'losses': 6, 'playerMethod': 'character', 'gamePlayed': 'Zelda'}
계산 사전에 키의 이름을 정의 14 + 1
{'playerName': 'peter', 'wins': 15, 'losses': 5, 'playerMethod':
'character', 'gamePlayed': 'GTA'}
에 플레이어 "피터을"새로운 승리의 카운트를 업데이트 한 후 사용자 입력을 통해 승리 (요청하신대로). 위의 진술의
gameTracker.append(
{inputString("Input wins variable name: "):inputInt("Input wins: ")}
)
print(gameTracker[0])
출력은 사용자 입력에 의해 키 이름을 정의
Input wins: 5
Input wins variable name: usr_peter_wns
{'usr_peter_wns': 5}
대부분의 시간/변수는 최선의 해결책이 아닙니다. 쉬운 조작을 위해 예측 가능한 이름을 갖는 것이 더 좋습니다. 당신에 대해 알 수있는 사전 *은 아직 알려져 있지 않다 무엇을 저장하기 위해 *
당신은 사용할 수 있습니다. 'd = {}; d [input_key] = input_value' –