#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])
解决问题的第一步就是准确地定义它。举个例子,说明你打算如何使用你的输赢追踪器,这将有助于明确你到底想做什么。在
您很可能不需要/不想通过用户输入定义变量名来存储wins。而是为每一组相关信息(例如来自一个用户的记录)创建一个字典。此词典将存储用户的名称、输赢计数等
我已经大致了解了你下面要找的东西。在
输出示例:
^{pr2}$在更新玩家“彼得”的新赢点数到14+1后
在字典中定义通过用户输入(根据您的请求)计算获胜数的键的名称。在
上述报表输出
大多数时候,通过用户输入/变量定义键名并不是最好的解决方案。最好有可预测的名称,以便于操作。在
相关问题 更多 >
编程相关推荐