我正在尝试创建一个程序,该程序将跟踪玩家的输赢率,现在,我正在尝试让该程序检查txt文件中是否列出了某个名称,但该名称始终返回false。
因此在内部def check_player
,当我输入名字时,它说它在列表中,但是我输入的所有后续名字都返回为false。.txt文件的内容是7个名称,我输入的名字也是列表中的名字,为什么它不检查整个文件以查看名称是否存在?
编辑:我发现一种更简单的方法来解决此问题。考虑这个关闭
def NewGame():
print("how many people are in this game?")
people = int(input())
for num in range(people): #does it n times where n is the number of players
print("who is the Player in this game?")
player = input()
file_name = "Player_List.TXT" #initializes the name of the file it is pulled from
check = check_player(file_name, player) #go to def check_player and pass file & player
if check == False:
print("this player is not in the system.")
print("would you like to add them(Y/N)?: ")
add = input()
if add == "Y" or "y":
AddPlayer()
def check_player(file_name, player):
""" Check if any line in the file contains given string """
# Open the file in read only mode
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if player in line:
return True
else:
return False
我认为您应该将“ return False”归为一个级别,然后删除其他级别。我认为现在是这样,check_player只看第一行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句