Python Forum
Learning python My lists are not coming out right
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning python My lists are not coming out right
#1
I am learning the python crash course book and really enjoying it. But on the if statements section where i have to write pizza toppings thongs are not working well.

I wondered if i am doing it wrong or something. I am using python 3

here is the code from the book

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:
	    if requested_topping == 'green peppers':
		    print('Sorry we are out of green peppers right now.')
else:
	    print('Adding ' + requested_topping + '.')
	
print('\nFinished making your pizza!')
in the book, it says it should come out like this

Output:
adding mushrooms sorry, we are out of green peppers right now. adding extra cheese
but for some reason, it misses out the adding mushroom bit.
and if i add the mushroom after the extra cheese then it adds the mushroom and leaves out extra cheese.

Is this common things are am i doing something wrong.
Reply
#2
Quote:
        if requested_topping == 'green peppers':
            print('Sorry we are out of green peppers right now.')
else:
        print('Adding ' + requested_topping + '.')
the if and the else need to be in the same indentation. As is right now the else is apart of the for block, you want it apart of the if block.
Recommended Tutorials:
Reply
#3
Thank you. i realized where i was going wrong.
Reply
#4
Just some points,Indentation in Python is 4-space and added f-string.
requested_toppings = ["mushrooms", "green peppers", "extra cheese"]
for requested_topping in requested_toppings:
    if requested_topping == "green peppers":
        print("Sorry we are out of green peppers right now.")
    else:
        print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!") 
Output:
Adding mushrooms. Sorry we are out of green peppers right now. Adding extra cheese. Finished making your pizza!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,609 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  const or #define coming from C jamie_01 11 3,361 Sep-16-2021, 05:39 PM
Last Post: jefsummers
Star --- Python lists and Linked Lists --- sabe 3 2,685 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,367 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  String coming up weird EddiesTech 1 1,517 Mar-15-2020, 05:02 PM
Last Post: buran
  Timer keeps coming up with a non-callable int object birdwatcher 1 3,162 Feb-16-2020, 12:40 PM
Last Post: DeaD_EyE
  Error Message Coming Up When Running Code eddywinch82 0 1,881 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  need help coming up with a code Staph 0 1,454 Jun-21-2019, 10:06 AM
Last Post: Staph
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,257 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020