def save_transaction(price, credit_card, description):
    file = open("transactions.txt", "a") # the "a" means you are always going to append to this file
    file.write("%s%07d%s\n" % (credit_card, price, description))
    file.close()


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: ")
        
        save_transaction(prices[choice - 1], credit_card, items[choice - 1])

In the program above I'M having trouble with a few lines of code. The fist is in the line choice = int(input("Choose an option: ")) Why must I use the same "choice" variable from my for loop? In the for loop choice holds the data for each element in items for each loop iteration. But now in holds a new value, a number selected by the user. I tried to change choice = int(input & if choice == option to something other than choice but it wouldn't run. And I don't understand why they must match up to the variable in the loop. The variable in the loop just temporarily hold the data for each element in items for each iteration. The second time the choice variable gets a new value, it's just compared to "option".

Last but not least, what is prices[choice -1] & items[choice - 1]?

Thanks for any all replies.

Question 1

You do not have to recycle your "choice" variable from your for loop to make the input work.

"choice" is being reassigned in this line:

choice = int(input("Choose an option: "))

If you change the later part of your program to this it will still work perfectly.

inputChoice = input("Choose an option: ") #Do not need int() as input defaults to int
    if inputChoice == option: 
        running = False
    else:
        credit_card = input("Credit card number: ")
        save_transaction(prices[inputChoice - 1], credit_card, items[inputChoice - 1])

Question 2

def save_transaction(price, credit_card, description):

The method save_transaction is given 3 varibles; price, credit card, and description.

save_transaction(prices[inputChoice - 1], credit_card, items[inputChoice - 1])

This line is calling to method save_transaction with your current card info.

prices[inputChoice - 1]

This line is looking at the chosen place in the prices list, the first item in a list is always at the 0 spot with the second item being at spot 1. This is why this program is taking your input minus 1. Using the [] is the way to index items from a list, if you wanted to grab the third item from the prices list you would type:

prices[2]

Which would return 1.80

This program is still not perfect and easy to crash with the wrong input. If you want to know how to use raw_input instead of input just ask.

From the way the code use int(input()) it looks that it is meant to use Python 3 where input is acting as raw_input in Python 2.

Yes, probably it is good idea to do some try..except with input here to not let the code stop with crash. See for example my number guess game snippet:
http://www.daniweb.com/code/snippet287950.html

inputChoice = input("Choose an option: ") #Do not need int() as input defaults to int

tbone2sk this code is for python 3.x,so he need int(input()).
Garrett85 make a note that you use python 3.x,most pepole use python 2.x so there is no confusion.

Last but not least, what is prices[choice -1] & items[choice - 1]?

>>> prices = [1.50, 2.0, 1.80, 1.20]
>>> prices[1]
2.0
>>> choice = 2
>>> prices[choice]
1.8
>>> #To get the second element -1(count not from 0 as almost all programming language)
>>> prices[choice -1]
2.0
>>>

Edited 6 Years Ago by snippsat: n/a

This article has been dead for over six months. Start a new discussion instead.