So the Text Adventure is lost. ;-;. I am working on a GUI to take a starting point and a destination input by the user, and scan a file to find a match for both. If it finds a match for Variable A + B then it will assign variable C based on more Data on the same row. For instance

Text File

Barnsley,Dronfield,1.70

When it gets to a , I want it to take the characters in the first block, and assign it to Variable (Var1a) and then go to the second one and do the same for Var2a, then do the following:

Check if inputUserStart = Var1a
Check if inputUserDestination = Var2a
 
if the statements are true, assign the last data block (1.70) to a variable (Var3)

Then to display all three like this:

LABELTITLE

Date Issued

Var1a
Var2a
Var3

How would I do this?
This program would print a simulation Bus Ticket. (BareBones)
Var 3 would be the Price of the ticket.

Sorry about your HDD. :(

Text File

Barnsley,Dronfield,1.70

When it gets to a , I want it to take the characters in the first block, and assign it to Variable (Var1a) and then go to the second one and do the same for Var2a, then do the following:

Check if inputUserStart = Var1a
 Check if inputUserDestination = Var2a
  
 if the statements are true, assign the last data block (1.70) to a variable (Var3)

Well, you could go char by char, but I like the Pythonic way:

data = "Barnsley,Dronfield,1.70"
vars = data.split(',') # splits string at comma delimiter into list
if len(vars) >= 3:    # have to check in case of malformed lines
   
   if vars[0] == inputUserStart and vars[1] == inputUserDestination:
       var3 = vars[2]

Then to display all three like this:

LABELTITLE

Date Issued

Var1a
Var2a
Var3

If you're using Tkinter, then just create a Frame and then Labels containing vars[0..3] within that Frame.

Jeff

By the way, how would I make it so that if there was no match, then it would continue scanning through, until it found a matching line?

By the way, how would I make it so that if there was no match, then it would continue scanning through, until it found a matching line?

So say you have your lines in a file f. Then you can read them in one-by-one:

for line in f:
   vars = line.split(',') # splits string at comma delimiter into list
  if len(vars) < 3:continue    # have to check in case of malformed lines
   
  if vars[0] == inputUserStart and vars[1] == inputUserDestination:
       var3 = vars[2]
       break
else:
  print "No match found.  Sorry!"

Jeff

NVM, I fixed the issue. It works now. Just need to do it in a child window! ^_^

It's doing what it needs to, but I need more data. Where can i find prices for standard bus tickets, such as a ticket from dronfield to sheffield costs 2.20? All I can find are special tickets, such as Dayriders and Megariders. I just need normal ticket prices to expand the data file. Stagecoach is the name of the service. Google isn't helpful.

My listbox is not working properly. Here's the code:

#Bus Ticket.py
"""A program that generates bus tickets based on data input by the user"""
from Tkinter import *
global passType
global start
global end
global price
class main:
    def __init__(self, master):
        self.master = root
        self.master.title('Bus Tickets')
        self.master.geometry('300x250+350+450')
        self.labelTitle = Label(self.master, text='Bus Tickets')
        self.labelTitle.pack(side=TOP)
        self.labelStart = Label(self.master, text='Where are you starting your Journey?')
        self.labelStart.pack(side=TOP)
        self.entryStart = Entry(self.master)
        self.entryStart.pack(side=TOP)
        self.labelDest = Label(self.master, text='Where do you want to go?')
        self.labelDest.pack(side=TOP)
        self.entryDest = Entry(self.master)
        self.entryDest.pack(side=TOP)
        self.labelList = Label(self.master, text='Choose the bus pass that you use:')
        self.labelList.pack(side=TOP)
        self.listPass = Listbox(self.master, height=3)
        self.listPass.pack(side=TOP)
        self.listPass.insert(END,"None")
        self.listPass.insert(END,"B-line")
        self.listPass.insert(END,"SHU Card")
        self.buttonOK = Button(self.master, text="OK", command=self.display)
        self.buttonOK.pack(side=BOTTOM)
        
        self.master.mainloop()
    def display(self):
        global start
        global end
        global price
        global passType
        start = self.entryStart.get()
        end = self.entryDest.get()
        passType=self.listPass.curselection()
        fileBus = open("businfo.txt", "r")
        
        for line in fileBus:
            variable = line.split(',')
            if len(variable) < 3:
                continue
            if variable[0] == start and variable[1] == end:
                price = variable[2]
                print variable[0]
                print variable[1]
                print price
                print passType
                child()
                break
            else:
                continue
        filepass = open("passinfo.txt", "r")
        for line in filepass:
            variable2 = line.split(',')
            if len(variable2) < 3:
                continue
            if variable2[0] == passType:
                print variable2[1]
                print variable2[2]
                passPrice = price*variable2[2]
                print passPrice
class child:
    def __init__(self):
        global start
        global end
        global price
        self.slave = Toplevel(root)
        self.label1 = Label(self.slave, text='Thompson Travel')
        self.label1.pack(side=TOP)
        self.labelBoard = Label(self.slave, text='Boarding point: '+start)
        self.labelBoard.pack(side=TOP)
        self.labelDest = Label(self.slave, text='Destination: '+end)
        self.labelDest.pack(side=TOP)
        self.labelCost = Label(self.slave, text='Price: '+price)
        self.labelCost.pack(side=TOP)
root = Tk()
main(root)

Example of a line in passinfo.txt:
1,B-Line,0.50

Help, please? It won't read properly. The output is:

Dronfield
Sheffield
2.20
('1',)

The businfo file hasn't changed.

You need to start a new thread with the proper title and give us the two files, so we can troubleshoot your program!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.