【问题标题】:saving a variable as a text file name将变量保存为文本文件名
【发布时间】:2022-01-23 12:06:29
【问题描述】:

我正在创建一个程序,该程序涉及将团队名称和统计信息存储到文本文件中,并且我想将文本文件称为用户输入的团队名称。目前它可以工作,但所有团队都存储在一个文本文件中。

代码:

import random

def TheTeams (name):
with open("the_teams.txt, "a+") as t:
        t.write (name)
        t.write ("\n")
        
        
def NewPlayer (pname):
    at = int(input("enter your players attack 1-10 "))
    de = int(input("enter your players defence 1-10 "))
    if at >10:
        print ("NO")
        at = random.randint(1,5)
    if de >10:
        print ("NO")
        de = random.randint(1,5)
        

        
    with open("the teamss.txt", "a") as t:
        t.write (pname)
        t.write (" ")
        t.write ('%d' % at)
        t.write (" ")
        t.write ('%d' % de)
        t.write ("\n")

for i in range (1,7):
    pname = input("enter your players name ")
    NewPlayer (pname)
    
print ("""      Welcome to the ice hockey game
you will create your own team and try to score against your opponent
your teams are stored for future use

""")

n_team1 = input("player 1 do you want to create a new team ")

if n_team1 == "yes":
    team = input("enter your team name ")
    TheTeams (team)

【问题讨论】:

    标签: python variables text-files filenames


    【解决方案1】:

    这是你码哥的更新版:

    import random
    
    def TheTeams (name):
      file_name = "./" + name + ".txt" # This is the code that will create the file
    
      with open(file_name, "a+") as t:
        t.write (name)
        t.write ("\n")
                
    def NewPlayer (pname):
      at = int(input("enter your players attack 1-10 "))
      de = int(input("enter your players defence 1-10 "))
      if at >10:
          print ("NO")
          at = random.randint(1,5)
      if de >10:
          print ("NO")
          de = random.randint(1,5)
              
      with open("the teamss.txt", "a") as t:
          t.write (pname)
          t.write (" ")
          t.write ('%d' % at)
          t.write (" ")
          t.write ('%d' % de)
          t.write ("\n")
    
      for i in range (1,7):
          pname = input("enter your players name ")
          NewPlayer (pname)
            
    print ("""      Welcome to the ice hockey game
    you will create your own team and try to score against your opponent
    your teams are stored for future use
    
    """)
        
    n_team1 = input("player 1 do you want to create a new team ")
    
    if n_team1 == "yes":
        team = input("enter your team name ")
        TheTeams (team)
    

    【讨论】:

      【解决方案2】:

      你可以为玩家的名字做同样的事情。请求输入并将其作为文件名的变量传递。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      • 在使用用户提供的名称创建或打开任何文件之前,您真的(是的,真的)想要清理输入 - 至少将其编码为 ascii 并删除任何特殊字符(更好 - 保留仅限字母数字字符)并限制可能的长度 - 否则很快就会出现安全问题。
      猜你喜欢
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      相关资源
      最近更新 更多