Issue
I am learning to program in Python 3 and I am working on a project that lets you purchase tickets to a/more movies. You first add the ticket to your shopping cart which is saved in a list and then you can view the items you have in your shopping cart.
My problem is that I don't know how to print the name of the film and the price which are in a dictionary
without replacing the first line.
#TODO La alegerea 3 daca scrii exit iti scrie "3" si nu inteleg de ce...
#TODO Daca nu scrii o cifra intre 1-4 la intrebarea de la meniu sa iti spuna "nu team inteles repeta"
import string
bani = [10]
punctuatie = str.maketrans('', '', string.punctuation)
cos = []
films = {
"Finding Dory": {"age": 3, "seats": 5, "price": 3},
"Bourne": {"age": 18, "seats": 5, "price": 4},
"Tarzan": {"age": 15, "seats": 5, "price": 5},
"Ghost Busters": {"age": 12, "seats": 5, "price": 6},
}
while True:
# meniu pentru client
meniu = "Bun venit la Cinema Fulgerul.\nTe rog sa faci o alegere tastant mai jos un numar din meniul acesta:\n1.Lista filme disponibile\n2.Bani tai\n3.Cumpara un bilet\n4.Cosul cu cumparaturi"
print(meniu)
# intrebarea ce vrei sa alegi
choice = int(input("Ce doresti sa acesezi, poti alege intre 1, 2, 3 sau 4: "))
back = "Pentru a reveni la meniu tasteaza exit: "
#daca alegi 1 se va intampla urmatoarele:
if choice == 1:
# printeaza doar filmele din films fara age,seats,price etc.
filme = str(list(films.keys()))
# scoate punctuatiile din printearea filmelor de la films.keys()
filmex = filme.translate(punctuatie)
# printeaza filmele disponibile
print("1.",filmex[0:12])
print("2.",filmex[13:19])
print("3.",filmex[20:27])
print("4.",filmex[27:-1])
input(back).split()
#daca scrie exit la intrebarea daca vrea sa revina la meniu il va aduce la meniu
elif back == "exit":
print (meniu)
print (choice)
#daca alege 2 se va intampla urmatoarele:
if choice == 2:
# ai la dispozitie 10 dolari
#bani = [10]
#printeaza mesajul cu cati bani ai valabili
print("Ai la dispozitie {}$.".format(*bani))
input(back).split()
if choice == 3:
#try:
print("Daca doresti sa vezi ce filme sunt valabile, scrie exit.")
bilet = str(input("Ce film doresti sa vizionezi?: ").title())
pret = films[bilet]["price"]
cumperi = input("Doresti sa adaugi in cosul de cumparaturi {}$ (y/n)?".format(bilet)).strip().lower()
if cumperi == "y":
cos.insert(0, bilet)
if choice == 4:
def numefilm():
print(*cos, sep="\n")
print (*cos, "- {}$".format(films[bilet]["price"]))
comanda = input("Doresti sa finalizezi comanda in total de: ")
With this code if I have 2 items in the shopping cart it prints smth like this : Bourne Tarzan - 4$
, but I don't want that, I want it to be like:
Tarzan - 5$
Bourne - 4$
Solution
You're printing out a whole list in the last print
statement, if you're using two list of same length to generate your output (looks like it), you can do this:
You can print after a list comprehension like this:
statements = ['{} - {}$'.format(x, films[x]['price']) for x in cos]
print('\n'.join(statements))
Answered By - Rocky Li
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.