Jump to content


Photo

Chapter 6


  • Please log in to reply
2 replies to this topic

#1 @$!-!i$!-!

@$!-!i$!-!

    Active Member

  • Members
  • PipPip
  • 13 posts
  • Gender:Male

Posted 08 December 2014 - 04:59 PM

Hi,

I was working on the code in the 6th Chapter. The code works fine for the 4 options on the menu but when I Input 5(Quit), I get an "IndexError: list index out of range" error.
Moreover, I do not see the transactions.txt file being created in the directory my python codes are in.
I am running Python 3.3.2

Following is the code:
from transactions import *
import promotion
import starbuzz

items = ["DONUT","LATTE","FILTER","MUFFIN"]
prices = [1.50,2.0,1.80,1.20]
running = True

while running:
    option = 1
    for choice in items:
        print(str(option)+"."+choice)
        option = option + 1
    print(str(option)+".Quit")
    choice = int(input("Choose an option: "))
    if choice == option:
        running = False
    else:
        credit_card = input("Credit card number: ")
        price = promotion.discount(prices[choice-1])
        if input("Starbuzz discount card? ") == 'Y':
            price = starbuzz.discount(price)
save_transaction(price,credit_card,items[choice-1])


#2 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 27 December 2014 - 12:57 AM


Dear, I am really sorry for a really late answer. :rolleyes:

Anyway the indentation of your save_transaction() function causing the problem. That one should be under the else statement.

save_transaction(price,credit_card,items[choice-1]) # Indentation of this line is the problem.


Have a look at the corrected code. :)

from transactions import *
import promotion
import starbuzz

items = ["DONUT","LATTE","FILTER","MUFFIN"]
prices = [1.50,2.0,1.80,1.20]
running = True

while running:
    option = 1
    for choice in items:
        print(str(option)+"."+choice)
        option = option + 1
    print(str(option)+".Quit")
    choice = int(input("Choose an option: "))
    if choice == option:
        running = False
    else:
        credit_card = input("Credit card number: ")
        price = promotion.discount(prices[choice-1])
        if input("Starbuzz discount card? ") == 'Y':
            price = starbuzz.discount(price)
        save_transaction(price,credit_card,items[choice-1]) # It will be here under the else statement.

B)

Edited by #TM#, 27 December 2014 - 01:03 AM.


#3 @$!-!i$!-!

@$!-!i$!-!

    Active Member

  • Members
  • PipPip
  • 13 posts
  • Gender:Male

Posted 27 December 2014 - 12:47 PM

It works.

Thank you, I should have tried this earlier.
:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users