简体   繁体   English

如何从文本文件中读取变量作为整数,修改所述变量然后将其存储在 Python 中的同一个文本文件中?

[英]How to read a variable from a text file as an integer, modify said variable then store it in the same text file in Python?

Beginner programmer here, so be weary of that.这里的初学者程序员,所以对此感到厌倦。 I'm currently working on a text-based turn-based battle "simulator" in Python.我目前正在使用 Python 开发基于文本的回合制战斗“模拟器”。 I want to add to my code a way to store and read the number of wins and losses the player has so that they can see these stats in the menu.我想在我的代码中添加一种方法来存储和读取玩家的输赢次数,以便他们可以在菜单中看到这些统计数据。 The turn-based part functions as intended, however the wins/losses stats (which I'm going to call "score" for the remaining explanation) do not.回合制部分按预期运行,但胜/负统计(我将在其余解释中将其称为“分数”)没有。 I have a text file named "wl.txt" with the score stored inside.我有一个名为“wl.txt”的文本文件,其中存储了分数。 It is two simple lines with the numbers of corresponding wins and losses, wins being on the first line and losses being on the second.这是两条简单的线,对应的输赢数量,赢在第一行,输在第二行。 As I want to update the score as the player advances, the two variables need to be integers for them to be updated accordingly.因为我想随着玩家的进步而更新分数,所以这两个变量需要是整数才能相应地更新。 To retrieve them from "wl.txt", I used the readline() method for wins and readlines() method for losses.为了从“wl.txt”中检索它们,我使用 readline() 方法获取胜利,使用 readlines() 方法获取损失。 However, as you probably know, readline() returns a str and readlines() returns a list, so I cannot update the value of the losses variable.但是,您可能知道, readline() 返回一个 str 而 readlines() 返回一个列表,所以我无法更新 loss 变量的值。 Here is a python file regrouping the code I use for this:这是一个 python 文件,它重新组合了我为此使用的代码:

def SeeScore():
    score = open("wl.txt", "r")
    print("Wins:",score.readline(), "\nLosses:", score.readlines())


score = open("wl.txt", "r")
wins = score.readline()
losses = score.readlines()
score.close()

wins += 2
losses += 5
score = open("wl.txt", "w")
score.write(str(wins)+"\n")
score.write(str(losses)+"\n")

SeeScore()

I have tried to do something like this:我试图做这样的事情:

wins = int(score.readline())
losses = int(score.readlines())

But you also probably know that you cannot turn a list into an integer.但是您可能也知道您不能将列表转换为整数。

I'm at a loss for what to do.我不知道该怎么办。

Thanks in advance.提前致谢。

For the losses you were using score.readlines which made it print ['1'] instead of just 1. Another issue is that you need to put int().对于您使用 score.readlines 的损失,它会打印 ['1'] 而不仅仅是 1。另一个问题是您需要输入 int()。

After testing with your code simply just replace使用您的代码进行测试后,只需替换

"\nLosses:", score.readlines())
# And
wins = score.readline()
losses = score.readlines()

With

"\nLosses:", score.readline())
# And
wins = int(score.readline())
losses = int(score.readline())

Final code:最终代码:

def SeeScore(wins, losses):
    print("Wins:",wins, "\nLosses:", losses)


score = open("wl.txt", "r")
wins = int(score.readline())
losses = int(score.readline())
score.close()



wins += 2
losses += 5
score = open("wl.txt", "w")
score.write(str(wins)+"\n")
score.write(str(losses)+"\n")

SeeScore(wins, losses)

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

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