r/CodingHelp • u/fashionableplantmom • Sep 02 '21
[Python] Python assignment. I can’t get it to calculate the cost of the movie based on the other movie variables. It keeps only giving me the 1st one as an answer. Any help would be appreciated
2
u/idetectanerd Sep 03 '21
Optimisation POV, In my opinion, you can put your show in a list, your cost in value pair in dictionary.
For loop it. It will be short.
The other folks already pointed out the error of why you don’t get desire answer.
3
u/skellious Intermediate Coder Sep 03 '21 edited Sep 04 '21
Yes, it would be better to put them in a dictionary. you can then do
child_price = 5.00 senior_price = 8.00 adult_price = 10.75 movie_in_3D = 15.00 movies = { "paw patrol": {"in_3D": True}, "shang-chi": {"in_3D": False}, "candyman": {"in_3D": False}, "free guy": {"in_3D": False}, "jungle cruise": {"in_3D": False} } print("Welcome to AMC theatres please select your movie!") print("movies currently showing:") for movie in movies.keys(): print(movie) no_movie_selected = True while no_movie_selected: what_movie = (input( "From the list above which movie will you be purchasing tickets for? ")).lower() if what_movie in movies.keys(): how_many_adult_tickets = int( input("How many adult tickets will you be purchasing? ")) how_many_child_tickets = int( input("How many children tickets will you be purchasing? ")) how_many_senior_tickets = int( input("How many senior tickets will you be purchasing? ")) if movies[what_movie]["in_3D"]: total = movie_in_3D * \ (how_many_adult_tickets + how_many_child_tickets+how_many_senior_tickets) else: total = adult_price*how_many_adult_tickets+child_price * \ how_many_child_tickets+senior_price*how_many_senior_tickets no_movie_selected = False print(f"Your total today is: {total}")2
u/fashionableplantmom Sep 04 '21
I greatly appreciate you for your thought-out response. I'm going to spend the whole weekend configuring it to work as a dictionary instead. Thank you so much
2
u/skellious Intermediate Coder Sep 04 '21 edited Sep 04 '21
No problem, happy to help. :) Please note I may have made some mistakes in the code as I wrote it late at night on my phone and didn't try to run it.
edit: ive edited it to make sure it works and ive included some other code to make it a complete thing, but this is just one way of doing it.
2
1
u/fashionableplantmom Sep 04 '21
that was the main thing I was having such a hard time with. understanding list. Thank you so much for this
2
u/idetectanerd Sep 04 '21
List is just an array of things you want.
Usually if I have a few item that I want to loop through, I will use a list let say mylist = item1,item2,item3
Then I will do a for loop to mylist.
So in your instance, you can loop your movies in the list to check through. But it really depend what kind of logic you want.
You can also do while to do all the task until the condition is false.
2
u/BlackCat550 Sep 03 '21
After you've completed the assignment, you should look into OOP.
It's a more advanced concept, but it will make this type of program easier to write, and more readable.
1
8
u/PekiDediOnur Intermediate Coder Sep 03 '21
variable == "something" or "whatever" doesn't do what you think it does, it's essentially (variable == "something") or "whatever" and whatever is being treated as True, so your statement will always be True.
Either do variable == "something" or variable == "whatever" or variable in ["something", "whatever", ...]
3
u/certainlyforgetful Sep 03 '21
Yep, the if on line 24, and the elif’s will always evaluate to true. The first section may be false but after the OR those are always true.
Idk specifically about python, but in other languages it’s basically just saying “yep you created a string!”
1
u/fashionableplantmom Sep 04 '21
I just want to thank everyone for the feedback it is greatly appreciated. I've struggled on this all week, lol It's not due till Tuesday so I will be defiantly fixing a lot of the issues with it. Again. Thank you so much
3
u/skellious Intermediate Coder Sep 03 '21
instead of the:
"Candyman" or "candyman" or "CANDYMAN"do this:
what_movie = (input("From the list above which movie will you be purchasing tickets for? ")).lower()
if what_movie == 'candyman":the .lower() will make the input all lower case, so it will match candyman, CanDyMaN, CANDYMAN, CanDYmaN or any other combination of letters.
2
u/Pr-Lambda Sep 03 '21
The line 24 means if what_movie is equal to "Paw Patrol" or if "paw patrol" is not an empty string or if "PAW PATROL" is not empty string.
2
u/iNqtural Sep 04 '21
Instead of using "what_movie == ...", use "what_movie.lower() ==". This will convert the string to lowercase.
3
u/Simple_Cheesecake_62 Sep 03 '21
instead of if what_movie == 'Paw Patrol' or "paw patrol" or "PAW PATROL": it should be if what_movie == 'Paw Patrol' or what_movie == "paw patrol" or what_movie == "PAW PATROL": as or "paw patrol" doesn't check anything hence it is always true
2
u/skellious Intermediate Coder Sep 03 '21
other than what I suggested in another comment, which would be the best solution in this situation, you can also do:
if what_movie in ["Paw Patrol", "paw patrol", "PAW PATROL"]:2
u/jamesbleslie Sep 03 '21
But I would go further and use
if what_movie.lower() = 'paw patrol' :2
u/skellious Intermediate Coder Sep 03 '21
Why? Then you have to use .lower() every time. It's better to use it once at the point of input as I said in my other comment.
1
u/jamesbleslie Sep 03 '21
Yeah, you're totally right. :) Sorry, I should have checked your other comment!
1
-5
Sep 03 '21
Make a function stop scripting lolz
4
u/skellious Intermediate Coder Sep 03 '21
OP clearly will not understand what you are attempting to say. please specify it in a less taunting fashion if you want it to be understood.
13
u/B1-102 Sep 02 '21
Please post the entire code as formatted text.