亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過用戶輸入檢查txt文件中是否存在多個字符串?

如何通過用戶輸入檢查txt文件中是否存在多個字符串?

慕妹3242003 2023-11-09 21:25:12
我正在嘗試創建一個程序來跟蹤玩家的勝負率,現在我正在嘗試讓程序檢查某個名稱是否列在 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
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

我認為你的“return False”應該降低一個級別,并將 else 刪除。我認為現在的情況是, check_player 只查看第一行。



查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號