我正在嘗試創建一個程序來跟蹤玩家的勝負率,現在我正在嘗試讓程序檢查某個名稱是否列在 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
添加回答
舉報
0/150
提交
取消