Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Need help with python 3 assignment

OpenStudy (anonymous):

@nsellers23 What is the assignment?

OpenStudy (anonymous):

I cannot promise that I can help, but I would give it a try

OpenStudy (anonymous):

tell us what you need

OpenStudy (anonymous):

This is my assignment

OpenStudy (anonymous):

and this is my data file i need to get information from

OpenStudy (anonymous):

I was sick for about a week with some virus so i missed all the lectures about getting information from a data file, so i tried to use the book to figure this out but i just cant seem to figure it out.

OpenStudy (anonymous):

I'll have a look

OpenStudy (anonymous):

thanks

OpenStudy (rsmith6559):

A quick file read: foo = open( "apr5.txt", 'r' ) for line in foo: #do stuff with line, separting the fields with tabs may be a good choice foo.close()

OpenStudy (anonymous):

why does this tell me that i need 'c' to be defined? ``` inFile = open('pa5.tickets','r') print() print('Customer Name' + 'Number' + 'Type' + 'Seat' + 'Price') print('------------------------------------------------------') for i in range(7): type = inFile.readline() type = type.strip() num = eval(inFile.readline()) if type == 'c': word = 'Child' bill = num * 17.50 if type == 'r': word = 'Reserved' bill = num * 27.50 elif type == 'g': word = 'General' bill = bill elif type == 'a': word = 'Adult' bill = num * 27.50 if type == 'r': word = 'Reserved' bill = num * 37.50 if type == 'g': word = 'General' bill = bill print(format(word,'20s'),format(num,'7.2f'),format(bill,'8.2f')) inFile.close() ```

OpenStudy (anonymous):

@e.mccormick can you come read this last question on this post and see if you see an answeR?

OpenStudy (anonymous):

you don't always know how long the file will be This one works in Python 2.7 and you need to brush up the print formatting ``` cPrice = 17.50 aPrice = 27.50 rAdd = 10 aMax = 0 aMaxCust = '' cMax = 0 cMaxCust = '' totalTickets = 0 Adult = 'Adult' Child = 'Child' reservedSeats = 'Reserved' generalSeats = 'General' ticketsales = open("tickets.txt", "r") sentinalValue = 'XXXX' print('Customer Name Number Type Seat Price') print('-------------------------------------------------------------') line = ticketsales.readline() while line[:4] != sentinalValue: customer = line line = ticketsales.readline() if line[0] =='a': ticketType = Adult else: ticketType = Child Reserved = ticketsales.readline()[0]=='r' if Reserved: Seats = reservedSeats else: Seats = generalSeats Number = int(ticketsales.readline()) totalTickets += Number if ticketType == Adult: Price = aPrice*Number if Seats>aMax: aMax=Number aCust=customer else: Price = cPrice*Number if Seats>cMax: cMax = Number cCust = customer if Reserved: Price += Number*rAdd print(customer,str(Number),ticketType,Seats,str(Price)) line = ticketsales.readline() ticketsales.close() print('-------------------------------------------------------------') print print('Statistics') print('----------') print('Total Tickets Sold: '), totalTickets print('Maximum Adult Tickets Sold: '), aMax, print(', by: '), aCust, print('Maximum Child Tickets Sold: '), cMax, print(', by: '), cCust ```

OpenStudy (anonymous):

alright let me see if i can get it to work on python3

OpenStudy (anonymous):

^i dont mean im just copying, i just mean ill use it to try and figure it out

OpenStudy (anonymous):

here is the ticket.txt (it is written in linux, an the program adds a \n after each input That's why I needed the indices

OpenStudy (anonymous):

@nsellers23 what do you mean by why does this tell me that i need 'c' to be defined? Yeh, you can adapt you're code if needed

OpenStudy (anonymous):

my output error says so, hold on

OpenStudy (anonymous):

``` Traceback (most recent call last): File "C:\Users\nicholas\Desktop\CMPS 150\Programming Assignments\Programming Assignment 5.py", line 21, in <module> num = eval(inFile.readline()) File "<string>", line 1, in <module> NameError: name 'c' is not defined ```

OpenStudy (anonymous):

Aren't you assigning the customer name to type?

OpenStudy (anonymous):

instead of the type?

OpenStudy (anonymous):

no its supposed to pull all the info i need out of the file

OpenStudy (anonymous):

including the customer name

OpenStudy (anonymous):

Yeh, but where is that happening?

OpenStudy (anonymous):

tbh im not really sure, i missed the lectures for this stuff so im just trying to figure it out on my own

OpenStudy (anonymous):

every time you have an inFile.readline() it reads the next line of the file so you have for i in range(7): type = inFile.readline() there the customer name is assigned to type you can check it by putting a print statement just after this line

OpenStudy (anonymous):

so will the for i in range(7): read all the lines of the file? i have an example im trying to use to help me and the data file has 30 lines of data(2 different kinds) and it has for i in range(15)

OpenStudy (e.mccormick):

If you look at the tickets file, you will see that there are 4 lines to each movie. So you will read in four lines each time you set up an entry.

OpenStudy (anonymous):

oooh so i should make it ``` for i in range(4) ``` ??

OpenStudy (anonymous):

no it checks if i < 7 if so it executes everything under the for then it adds 1 to i and goes back to the for and checks if i is still < 7 etc

OpenStudy (anonymous):

I think a while loop works better and have it stop if it encounters the sentinal value: that is the 'XXXXX'

OpenStudy (anonymous):

hm thats not a bad idea

OpenStudy (anonymous):

within the while loop you can read the customer data for each customer that is a inFile.readline() fro customer, seatType, ReservedType, and Number so 4 times it repeats for every customer that way and stops when XXXXX is encountered

OpenStudy (e.mccormick):

Well, I would sentinel on the x instead of c/a or the -99 rather than the XXXXX. That is because you could end up with a movie that is named XXXXX. But oh well, that is a choice they made in the instructions. Since XXX was a movie, I think their choice is poor, but you need to follow that instruction. Also, as the instruction says sentinel, which does indicate a while of some sort is needed.

OpenStudy (anonymous):

I would too, but in the assignment that was explicitly mentioned 'The sentinel value will be a customer name of ‘XXXXX’ (all capital letters)'

OpenStudy (anonymous):

But maybe I'm interpreting that too stricktly

OpenStudy (e.mccormick):

As for the book, here is another option: http://www.tutorialspoint.com/python/python_files_io.htm That is I/O in general. And then... http://www.tutorialspoint.com/python/file_readline.htm readline() to pull in each line as a string.

OpenStudy (e.mccormick):

@abtster No, when they make a specific instruction like thatm even a poor one, you have to follow it. I was just adding that part to point out a flaw in the instructions to keep in mind for the future.

OpenStudy (anonymous):

ill be back in a few minutes, this gonna take a while and already sleepy, gonna have to go get an energy drink. thank yuo for the help so far btw everyone

OpenStudy (anonymous):

The assignment also states As you read through the file, print a “neat and tidy” table of the data read for the customer .... so it they probably want the input line by line, and not to read everything in first and then cycle through the full input

OpenStudy (anonymous):

Why do i get this error? ``` Traceback (most recent call last): File "C:\Users\nicholas\Desktop\CMPS 150\Programming Assignments\pa5 final attem[t.py", line 7, in <module> ticType = ticType.strip() NameError: name 'ticType' is not defined ``` from this code ``` inFile = open("pa5.tickets","r") for i in range(7): name = inFile.readline() ticType = ticType.strip() seat = seat.strip() number = eval(inFile.readfileline()) if ticType == 'c': perPrice = 17.50 ticType = 'Child' seat = 'General' if ticType == 'c': perPrice = 27.50 ticType = 'Child' seat = 'Reserved' if ticType == 'a': perPrice = 27.50 ticType = 'Adult' seat = 'General' if ticType == 'a': perPrice = 37.50 ticType = 'Adult' seat = 'Reserved' ```

OpenStudy (anonymous):

The if statements and blocks should be idented one level further they should be in the block of code under the for statement

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!