简体   繁体   English

读取文件并检查数据是否在文件中。蟒蛇

[英]Reading files and checking if data is in the file. Python

I'm making a dice game for a school project. 我正在为一个学校项目制作一个骰子游戏。 When you start the game you input your name and it need to read from the file "Player_Names.txt" the list of names of players who played before if the name isn't on the list then they get a "welcome" but if it is they get a "welcome back". 当你开始游戏时,你输入你的名字,它需要从文件“Player_Names.txt”中读取之前玩过的玩家的名字列表,如果名字不在列表上,那么他们会得到“欢迎”,但如果它他们得到了“欢迎回来”。

With my current code what happens is it only reads the 1st line in the file so if the name isn't on the 1st line it'll give back a message for welcoming a new player. 使用我当前的代码会发生什么,它只读取文件中的第一行,所以如果名称不在第一行,它将给出一个欢迎新玩家的消息。 Also if its part of a name so if you first enter "Matthew" and then another time enter "Matt" it will give you a "welcome back" message but "Matt" is a different person so it should be a "welcome" message. 另外,如果你的名字的一部分,如果你第一次输入“马修”,然后另一次输入“马特”它会给你一个“欢迎回来”的消息,但“马特”是一个不同的人所以它应该是一个“欢迎”的消息。 Also if the Name you enter is on the list but on the 2nd line in the file you get nothing back the program just continues to the next line of code. 此外,如果您输入的名称在列表中,但在文件的第二行,您没有得到任何回复程序只是继续下一行代码。

Names = open("Player_Names.txt", "r+")  
player1 = input("Enter your name: ")  
if player1 in Names.readline():  
    print("Welcome back you are player 1")  
elif player1 not in Names.readline():  
    print("Welcome you are player 1")  
    Names.write(player1)  
    Names.write("\n")  

How can I get the programe to read all the lines and treat the words inputed as whole words not letters like in the "Matthew" example? 我怎样才能让程序阅读所有的行并将整个单词作为整个单词处理,而不是像“马太”这样的单词?

You need to read all the lines, with readline() you're reading just one... 你需要读取所有的行, readline()你只读一个...

Names = open("Player_Names.txt", "r+")  
content = Names.readlines()

content = [x.strip() for x in content] 

if player1 in content:  
    print("Welcome back you are player 1")  
else: 
    print("Welcome you are player 1")  
    Names.write(player1)  
    Names.write("\n")  

Several issues here: 这里有几个问题:

if player1 in Names.readline():  
    print("Welcome back you are player 1")  
elif player1 not in Names.readline():  

this construct is normally redundant, because first condition is the negation of the second so you could write: 这个结构通常是多余的,因为第一个条件是第二个条件的否定所以你可以写:

if player1 in Names.readline():  
    print("Welcome back you are player 1")  
else:

but in that case Names.readline() has the side effect of consuming the first line. 但在这种情况下, Names.readline()具有消耗第一行的副作用。 So they're not equivalent. 所以他们不等同。

Besides, if you have several lines in your file, your algorithm doesn't work. 此外,如果文件中有多行,则算法不起作用。

I would create a list with the lines and use any : 我会用行创建一个list并使用any

lines = Names.readlines()
if any(player1 in line for line in lines):
   # known player
else:
   # new player

note that you can create a more performant lookup using a set and exact match: 请注意,您可以使用set和完全匹配创建更高性能的查找:

lines = {x.strip() for x in Names}
player1 = player1.strip()
if player1 in lines:
   ....
else:

You need to read the whole file and split it by newlines, this way you will get the list of lines and match will compare against full name. 您需要读取整个文件并按换行符拆分,这样您将获得行列表,匹配将与全名进行比较。 So second line should be 所以第二行应该是

if player1 in Names.read().split('\n'):  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM