Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 创建用户和附加统计信息_Python 3.x - Fatal编程技术网

Python 3.x 创建用户和附加统计信息

Python 3.x 创建用户和附加统计信息,python-3.x,Python 3.x,我在游戏项目中遇到了一些问题,试图在用户开始新游戏时创建一个用户创建提示,如果输入的用户名尚未退出,则创建一个新用户,如果用户名已经存在,则添加统计信息。那些附加到用户统计信息中的统计信息包括赢、输、平 所以我只能想到两个选项,第一个是创建一个以用户名为键的字典,并为它们的stat值创建一个嵌套列表,或者创建一个包含用户名函数和每个列表的函数的类,比如wins list、loss list等等 任何帮助或想法都将不胜感激 1月28日编辑: 根据对代码的要求,我将发布我所拥有的和我希望实现的部分

我在游戏项目中遇到了一些问题,试图在用户开始新游戏时创建一个用户创建提示,如果输入的用户名尚未退出,则创建一个新用户,如果用户名已经存在,则添加统计信息。那些附加到用户统计信息中的统计信息包括赢、输、平

所以我只能想到两个选项,第一个是创建一个以用户名为键的字典,并为它们的stat值创建一个嵌套列表,或者创建一个包含用户名函数和每个列表的函数的类,比如wins list、loss list等等

任何帮助或想法都将不胜感激

1月28日编辑:

根据对代码的要求,我将发布我所拥有的和我希望实现的部分

第1部分-用户名创建:

    while True:
  playerinput = input("Please type a number correlating with your menu choice, or 4 to return to main menu: ")

  if playerinput == "1":
      game_range = range(1, 10)
      play_the_game(playerinput, game_range)
  elif playerinput == "2":
      game_range = range(1, 26)
      play_the_game(playerinput, game_range)
  elif playerinput == "3":
      game_range = range(1, 28)
      play_the_game(playerinput, game_range)
  elif playerinput == "4":
      exec(open("menuitems.py").read(), globals())
  else:
      print("Unknown input: ", playerinput, ", please try again")
在开始一个新游戏的这一点上(我把菜单项留在这里以减少代码大小),通过选择一个菜单选项,我想让它在这一点上要求用户输入用户名,如果它不存在,则创建一个新用户,或者用统计数据附加到它

第2部分-用户统计(这是代码的一大部分):

我省略了代码中使用的很多函数来再次尝试保持这个小值,但是我想要完成的是,如果玩家的“赢的条件”得到满足,那么它会将+1添加到用户的赢的统计数据中,如果“抽的条件”,那么+1添加到抽的统计数据中,如果计算机赢了,那么+1添加到输的统计数据中

我有另一部分的菜单代码,玩家可以选择检查用户的统计数据,在那里我想打印用户名,他们的赢,输和平局


到目前为止,我认为字典是最好的选择,如果字典中存在此用户名,则使用此键并附加到其值,否则创建新键,等等。

更新:好消息!我想到了一个解决我的问题的方法,实际上是一个非常简单的方法,看起来像这样

with open("users.json", "r") as f:
  try:
    user_dict = json.load(f)
  except ValueError:
    user_dict = {}
username = input("Please enter a username to create a new user, or to access an existing user: ")
if username not in user_dict:
  user_dict[username] = [0, 0, 0]
    with open("users.json", "w") as f:
      json.dump(user_dict, f)
然后在赢、平、输等之后,我在休息前的每个赢/输条件等之后将此代码添加到游戏代码中

                if win_condition(board, player_icon, playerinput):
                  print_the_board(board, playerinput)
                  print("Congrats! You beat that pesky computer!")
                  user_dict[username][0] += 1
                  game_in_play = False
在添加了“user_dict[username][index]+=1”之后,我将只使用索引0表示赢,索引1表示输,3表示平局

我现在在函数中添加了一些代码,当游戏结束时会触发,看起来像这样

with open("users.json", "r") as f:
  try:
    user_dict = json.load(f)
  except ValueError:
    user_dict = {}
username = input("Please enter a username to create a new user, or to access an existing user: ")
if username not in user_dict:
  user_dict[username] = [0, 0, 0]
    with open("users.json", "w") as f:
      json.dump(user_dict, f)

现在,它可以完美地处理与用户名相关的持久性统计数据,感谢Stack为我在这里找到的JSON命令…

请向我们展示您的代码、您尝试过的内容、输入内容、输出内容以及预期的输出内容。