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.