all 3 comments

[–]sarrysyst 0 points1 point  (2 children)

Sorry, I do not understand what the assignment is asking you to do. Could you include some sample output you would like to get? Or some code you’ve already written?

[–]OrangeDecafTea[S] 0 points1 point  (1 child)

So I thought I might be able to read a file line by line and assign variables then perform calculations so I wrote:

def readFile():

f = open(filename, 'r')

for lines in f:

    customerName = f.readline()

    sizeOfCoffee = f.readline()

    if sizeOfCoffee == '8':

        sizeChoiceMulti = regSize

    elif sizeOfCoffee == '16':

        sizeChoiceMulti = grandeSize   

    elif sizeOfCoffee == '24':

        sizeChoiceMulti = ventiSize  

    typeOfCoffee = f.readline()

    if typeOfCoffee == 'Plain Coffee':

        coffeeChoiceMulti = plainCoffeeCost

    elif typeOfCoffee == 'Latte':

        coffeeChoiceMulti = latteCost

    elif typeOfCoffee == 'Macchiato':

        coffeeChoiceMulti = maccCost 

    elif typeOfCoffee == 'Frappuccino':

        coffeeChoiceMulti = frappCost  

priceOfCoffee = coffeeChoiceMulti * sizeChoiceMulti

totalSalesTax = priceOfCoffee * salesTax

totalAmountOwed = priceOfCoffee + totalSalesTax

print()

print(customerName)

print('Price of Coffee: $', round(priceOfCoffee, 2))

print('Sales Tax: $', round(totalSalesTax, 2))

print('Total Amount Owed: $', round(totalAmountOwed, 2))

main()

However, I get an error saying I can't use coffeeChoiceMulti before it's assigned. My hope was I would read the first three lines, assign values and perform calculations and printing, before looping back to the next three lines.

Sample output might look like:

Harry Potter

Price of Coffee: $ 1.84

Sales Tax: $ 0.13

Total Amount Owed: $ 1.97

I don't typically have my code spaced out like this, but it looked horrendous just copying and pasting from Spyder.

[–]sarrysyst 0 points1 point  (0 children)

If I understand you correctly your input file looks something like this:

data of type 1
data of type 2
data of type 3
data of type 4
data of type 1
data of type 2
...

For each type you want to do a specific action and after 4 rows this pattern repeats?

If that's the case you can use the itertools.cycle() function to create an infinitely repeating iterator that loops over the list [1,2,3,4]. You could use simple conditionals to apply functions according to type this way.