Exercise 1 – Python, Lets see what you have learn till now. I am giving you a script/code and I believe you will be able to understand that code without running it.

You need to understand that code technically and logically. What that code do? and how?

Just copy and past the code in your file. I will explain what is going on (a bit).

Code/Script

# Simple order function to take input and write in file
def order (var1, var2, var3):
    orderbook = open("orderbook.txt", "w")
    orderbook.write("Order from: %s\nPizza type: %s\nPizza Size: %s\n" % (var1, var2, var3))
    orderbook.close()
# Function to select size
def Pizzasize():
    psize = input("Select size\n1.)Small\n2.)Medium\n3.)Large\n>")
    if psize == "1":
        psize = "Small"
    else:
        if psize == "2":
            psize = "Medium"
        else:
            if psize == "3":
                psize = "Large"
            else:
                input("Wrong Choice Hit enter to exit")
                exit()
    return psize
# Start of Main script
print ("""
***************************************
**** Welcome to online pizza store ****
***************************************
""")
Name = input ("Please enter your name: ")
ptype = input ("Select your pizza type\n1.) Veg\n2.) Non Veg\n>")
if ptype == "1":
    psize = Pizzasize()
    pizza = input("""Select your Pizza
1.) Simple Veg
2.) Simple Veg with double topping\n>
""")
    print ("Thanks for your order: ")
    if pizza == "1":
        order(Name, "Simple Veg", psize)
    else:
        if pizza == "2":
            order(Name, "Simple Veg with double topping", psize)
        else:
            input("Wrong Choice hit enter to exit")
            exit()
else:
    if ptype == "2":
        psize = Pizzasize()
        pizza = input("""Select your Pizza
1.) Simple Non Veg
2.) Simple Non Veg with double topping\n>
""")
        print ("Thanks for your order: ")
        if pizza == "1":
            order(Name, "Simple Non Veg", psize)
        else:
            if pizza == "2":
                order(Name, "Simple Non Veg with double topping", psize)
            else:
                input("Wrong Choice hit enter to exit")
                exit()
    else:
        input("Wront Choice hit enter to exit")
        exit()

Now run the code and see the output in file orderbook.txt. Your script will create this file with all orders.

Little Explanation:

  • We have created 2 functions to avoid multiple writing of that script, as that script will be same in both cases
  • Last “else” is in case if user enter any wrong option.

Exercise of the day:

  • Use “a” append instead of “w” write while opening file. See the difference in result. You need to run program multiple time to see difference.
  • Add 2 options in menu and change code as per need.

 

comment (1)

Leave me comment