Home > Net >  How can I keep this line short in python .Please help anyone
How can I keep this line short in python .Please help anyone

Time:08-15

I would like to keep if statement line short .Is there any way?


    pizza = input("Welcome to Python pizza deliveries.\nWhich pizza would you like to order. S M or L")
    bill = 0
    if pizza == "s" or pizza == "S" or pizza == "m" or pizza == "M" or pizza == "l" or pizza == "L":
        if pizza == "s" or pizza == "S":
            print("You ordered small pizza. That costs $15.")
            bill  = 15
        elif pizza == "m" or pizza == "M":
            print("You ordered medium pizza. That costs $20.")
            bill  = 20
        elif pizza == "l" or pizza == "L":
            print("You ordered Large pizza. That costs $25.")
            bill  = 25
        # add pepperoni for small pizza $2 for mediun and large $3
        pepperoni = input("Do you like to add pepperoni on your pizza? Y or N")
        if pepperoni =="Y" or pepperoni =="y":
            if pizza == "s" or pizza == "S":
                bill  = 2
            else:
                bill  = 3
        #  for extra cheese  $1.
        extra_cheese = input("Do you like to add extra_cheese on your pizza? Y or N")
        if extra_cheese == "Y" or extra_cheese == "y":
            bill  = 1
        print(f"Your total bill is: {bill}")
    else:
        print("Wrong keyword. Try again.")

This line I wanna keep it short or wanna know to write another way. Is there any one who can help me? I would be pleased to know.

if pizza == "s" or pizza == "S" or pizza == "m" or pizza == "M" or pizza == "l" or pizza == "L":

CodePudding user response:

you could just do

if pizza.lower() in ["s","m","l"]:
   do_something()

BUT I suggest modifying your code in another way: move the else statement upwards and avoid even checking the letters condition twice. just return from the order handler if it's the wrong value. (you could also take the input before calling the order handler and send it as a parameter.)

def order_handler():
    pizza = input("Welcome to Paython pizza deliveries.\nWhich pizza would you like to order. S M or L")
    bill = 0
    if pizza == "s" or pizza == "S":
        print("You ordered small pizza. That costs $15.")
        bill  = 15
    elif pizza == "m" or pizza == "M":
        print("You ordered medium pizza. That costs $20.")
        bill  = 20
    elif pizza == "l" or pizza == "L":
        print("You ordered Large pizza. That costs $25.")
        bill  = 25
    else:
        print("Wrong keyword. Try again.")
        return
    # add pepperoni for small pizza $2 for mediun and large $3
    pepperoni = input("Do you like to add pepperoni on your pizza? Y or N")
    if pepperoni == "Y" or pepperoni == "y":
        if pizza == "s" or pizza == "S":
            bill  = 2
        else:
            bill  = 3
    #  for extra cheese  $1.
    extra_cheese = input("Do you like to add extra_cheese on your pizza? Y or N")
    if extra_cheese == "Y" or extra_cheese == "y":
        bill  = 1
    print(f"Your total bill is: {bill}")

CodePudding user response:

Create a list containing those letters and then check if pizza in list

myList = ["s","S","m","M","L","l"]
if pizza in myList:
   do something

CodePudding user response:

You could write it like this:

if pizza.lower() in [*"sml"]:

  • Related