Vlatko77
Vlatko77

Reputation: 15

Python Can't find string in file

I started working on my game few days ago. I made system for loading(on start) and saving(on exit). On first loging in everything is fine but on second error(NameError: name 'strange' is not defined) showed up. Can someone help me to solve it please? I know that problem is in finding name in file becouse I tryed to put else statment after everything under

elif ab.read().find(a) != -1:    

and that what I put under else worked but it was just print so other required didn't worked under else. Here's my program:

import sys, random, time, re
print("Welcome to game Special Travel Adventure!")
def con(a, b):
    return a.lower() == b.lower()
a = str(input("If you want to continue with game type your name here: "))
ab = open("STAPlayers.txt", "r+")
if ab.read().find(a) == -1:
    if ab.read() == "":
        ac = "Name:" + a + ":Strange:" + "0" + ":Courage:" + "0" + ":Skills:" + "0" + ":Money:" + "0" + ":Level:" + "0" + ":Deaths:" + "0"
        ab.write(ac)
        strange = 0 
        courage = 0
        skills = 0
        money = 0
        level = 0
        deaths = 0
    else:
        ac = "\nName:" + a + ":Strange:" + "0" + ":Courage:" + "0" + ":Skills:" + "0" + ":Money:" + "0" + ":Level:" + "0" + ":Deaths:" + "0"
        ab.write(ac)
        strange = 0 
        courage = 0
        skills = 0
        money = 0
        level = 0
        deaths = 0
elif ab.read().find(a) != -1:
    readdd = ab.readlines()
    for line in readdd:
        if line.find(a) != -1:
            zm = line.split(":")
            zm.remove("Name")
            zm.remove("Strange")
            zm.remove("Courage")
            zm.remove("Skills")
            zm.remove("Money")
            zm.remove("Level")
            zm.remove("Deaths")
            strange = int(zm[1]) 
            courage = int(zm[2])
            skills = int(zm[3])
            money = int(zm[4])
            level = int(zm[5])
            deaths = int(zm[6])
ab.close()
def levelc():
    if courage and strange and skills == 1:
        level += 1
        return True
    if courage and strange and skills == 2:
        level += 1
        return True
    if courage and strange and skills == 3:
        level += 1
        return True
    if courage and strange and skills == 4:
        level += 1
        return True
    if courage and strange and skills == 5:
        level += 1
        return True
    else:
        return False
b = input("Start Menu\nSelect: Start, Upgrades, Exit. ")
while b != "dont save":
    if con(b, "Exit"):
        aj = open("STAPlayers.txt", "r")
        lines = aj.readlines()
        aj.close()
        aj = open("STAPlayers.txt", "w")
        jmj = "Name:" + a + ":Strange:" + str(strange) + ":Courage:" + str(courage) + ":Skills:" + str(skills) + ":Money:" + str(money) + ":Level:" + str(level) + ":Deaths:" + str(deaths)
        for linee in lines:
            if str(a) not in linee:
                aj.write(linee)
            elif str(a) in linee:
                jmjm = jmj + "\n"
                aj.write(jmjm)
        aj.close()
        sys.exit()
        break

I know that problem is in finding name in file becouse I tryed to put else statment after everything under

elif ab.read().find(a) != -1:    

and that what I put under else worked but it was just print so other required didn't worked under else. Please help.

Upvotes: 0

Views: 289

Answers (1)

SethMMorton
SethMMorton

Reputation: 48735

Your problem is you are trying to read the file twice. You can only read a file once (without resetting to the beginning of the file). Try this:

ab_read = ab.read()
if ab_read.find(a) == -1:
    ...
elif ab_read.find(a) != -1:
    ...

Although, you might be better off using if a in ab_read: and then an else. Also, you might consider more meaningful variable names than ab or a.


To answer the next question you will have: your call to readdd = ab.readlines() will fail for the same reason multiple calls to ab.read() fail. You can get the same information with readdd = ab_read.splitlines()


In general, you are reading files way too often. Reading from files is a slow and resource consuming operation in any language. You chould consider reading the same file multiple times a very poor programming practice. You should refactor your code to only read each file once, and that should fix most of your issues.

Upvotes: 3

Related Questions