0

I'm making a pizza operator program for school. what is basically does is it asks for the customers name, asks if you want pickup or delivery etc. The problems I have encountered is that when I don't type anything into the input it will give the error to make you input it but then will automatically stop the program. I would like it to repeat itself. and also I have a bug in get_user_info when I click 1, 2 or 3 it does nothing but go in a infinite loop which I cannot seem to solve. I hope you guys could help me thank you very much in advance.

Regards, Johnathon

My code

premium_pizzas = ["Supreme Cheese", "The Legendary pizza", "Pentakill supreme", "Teeto shroomo supreme", "The volcanic rengar", "Cheese and Ham" , "Vegetriano" ]
gourmet_pizzas = ["Flame Gorrila", "Snazzy chicken", "Intergalactic BBQ", "BBQ Chicken"]

#premium_pizzas = 8.50
#gourmet_pizzas = 5.00

customer_name = ""
def get_customer_name():
  customer_name =input("what is your name?\n\t")
  if customer_name is "":
      print("Error please enter a name!\n\t")
  #else: get_user_info()


def get_delivery_details():
    get_address = input("Please enter a delivery address\n\t:")
    if get_address == "":
        print("error you must enter a address")

    get_phone_number = str(input("please enter your phone number"))
    if get_phone_number is "":
        print("Input must be an integer(numbers only)")
    if  get_phone_number is "abcdefghijklmnopqrstuvwxyz":
        print("Input must be an integer(numbers only)")
    else:
     get_pizza_list()




def get_pizza_list():
    for i in range (0,6):
        None




def get_user_info():
    while not get_user_info == "0":
     user_input=str(input("Press 1 for delivery press\nPress 2 for pickup\nPress 3 to exit\n\t:"))
    if get_user_info == "1":
        get_delivery_details()

    elif get_user_info == "2":
        get_pizza_list()

    elif get_user_info == "3":
        exit






#get_user_info()
get_customer_name
#get_delivery_details()
#get_pizza_list()

3 Answers3

0

You want to loop as long as the customer name is empty:

customer_name = ""
def get_customer_name():
    while (customer_name == ''):
        customer_name = raw_input("what is your name?\n\t")
        if customer_name is "":
            print("Error please enter a name!\n\t")

In addition, I would re-think the use of global variables. Programs using globals do not scale well.

def get_customer_name():
    result = ''
    while (result is ''):
        result = raw_input("what is your name?\n\t")
        if (result is ''):
            print("Error please enter a name!\n\t")
    return(result)

and later in the program:

 customer_name = get_customer_name()
virtualnobi
  • 1,140
  • 2
  • 11
  • 35
  • Redundant parentheses on your while loops (and on some if statements also). " if (results is ''): " could be rewritten to " if not result: ". – cpb2 Jun 03 '14 at 09:00
  • Sorry, I believed too much in what was there: `input()` does something weird - I am usually using `raw_input()`, which works fine when replaced. And at least in my Python installation, `get_customer_name()` returns a String, as programmed by `return(result)`. That's one difference between the OP and my version. If your Python returns `None`, I'd be really worried ;-) – virtualnobi Jun 03 '14 at 09:10
  • I like redundancy. "Thus, programs must be written for people to read, and only incidentally for machines to execute." Alan.J.Perlis in Abelson,H. & Sussman,G. (1985): The structure and interpretation of computer programs, Preface – virtualnobi Jun 03 '14 at 09:13
  • Implicit is better than explicit, indeed. But simple is better than complex! ;') – cpb2 Jun 03 '14 at 10:16
0

The first problem: I would change

  customer_name =input("what is your name?\n\t")
  if customer_name is "":
       print("Error please enter a name!\n\t")

with

  customer_name=""
  while customer_name == "":
       customer_name =input("what is your name?\n\t")
       print("Error please enter a name!\n\t")

And the other problem... quick suggestion: are you sure you wanna read user_input and use get_user_info as a variable? Just use user_input, not the name of the function :) ^^

Editing so not to pollute the edit place:

while not user_input == "3":
  user_input=str(input("Press 1 for delivery press\nPress 2 for pickup\nPress 3 to exit\n\t:"))
  if user_input == "1":
        get_delivery_details()

  else user_input == "2":
        get_pizza_list()

Basically you cycle until user_input is different from 3, the (main) problem with your solution was that you were using a bogus variable (get_user_info) to perform the check to let the cycle end, BUT you were setting a different one (user_input) ;)

Davide
  • 498
  • 4
  • 12
0

1. Use raw_input()

2. Work with objects/classes. It'll make your program more readable/debugable/organized.

3. Some of your if statements could be rewritten.

4. Make global variables uppercase for readability. (actually not sure if this in PEP-8 but I always do it)

Example

Here is something I made. It is dirty but it works.

PREMIUM_PIZZAS = ["Supreme Cheese", "The Legendary pizza", "Pentakill supreme", "Teeto shroomo supreme", "The volcanic rengar", "Cheese and Ham" , "Vegetriano" ]
GOURMET_PIZZAS = ["Flame Gorrila", "Snazzy chicken", "Intergalactic BBQ", "BBQ Chicken"]


class Customer():
    def __init__(self):
        self.name = ''
        self.address = ''
        self.phone = ''
        self.pickup = False
        self.pizza = ''

class Order():
    def __init__(self):
        self._customer = Customer()

    def get_order(self):
        if not self.get_customer_name():
            return None

        if not self.get_user_info():
            return None

        return self._customer

    def get_customer_name(self):
        name = raw_input("what is your name?\n\t")

        if not name:
            print("Please enter a name!\n\t")
        else:
            self._customer.name = name
            return True

    def get_delivery_details(self):
        address = raw_input("Please enter a delivery address\n\t:")

        if not address:
            print("You must enter a address")
            return None

        self._customer.address = address

        phone_number = raw_input("Please enter your phone number\n\t")
        try:
            self._customer.phone = int(phone_number)
        except:
            print("Input must be an integer(numbers only)")
            return None

        pizza_choice = self.get_pizza_list()
        if not pizza_choice:
            return None

        return True

    def get_pizza_list(self):
        # if anything went wrong, return None
        # get a listing of the pizzas here, etc
        choice = PREMIUM_PIZZAS[1]

        if choice:
            self._customer.pizza = choice
            return True

    def get_user_info(self):
        user_choice = raw_input("Press 1 for delivery press\nPress 2 for pickup\nPress 3 to exit\n\t:")

        if user_choice == "1":
            if self.get_delivery_details():
                return True

        elif user_choice == "2":
            self._customer.pickup = True

            if self.get_pizza_list():
                return True



while True:
    print '--\nWelcome, please order a pizza!'

    order = Order()
    info = order.get_order()

    print '--'
    if info:
        for attr in [a for a in dir(info) if not a.startswith('__')]:
            print '%s: %s' % (attr ,getattr(info, attr))

Output

    dsc:~/bla$ python test.py
--
Welcome, please order a pizza!
what is your name?
    Sander Ferdinand
Press 1 for delivery press
Press 2 for pickup
Press 3 to exit
    :1
Please enter a delivery address
    :The Netherlands
Please enter your phone number
    8349644343         
--
address: The Netherlands
name: Sander Ferdinand
phone: 8349644343
pickup: False
pizza: The Legendary pizza
--
Welcome, please order a pizza!
what is your name?

You might want to use my snippet as an example for making a class that contains all the pizzas and their individual prizes and implement that in!

cpb2
  • 798
  • 1
  • 9
  • 17
  • This did help me, I am not allowed to use classes. Since we aren't in the expert level with python yet. Classes will just drive the teacher off to suspicion. – user3702248 Jun 05 '14 at 04:33
  • Did the other comments help you though or are you still having problems? (I don't see an answer in this thread) – cpb2 Jun 05 '14 at 09:00
  • yes I cannot get it to make it so the phone numbers input is integer only, I want it so I can type in hi and it will give an error. – user3702248 Jun 07 '14 at 14:10
  • Look at my example code above. My code for setting 'self._customer.phone' is in a try except block. If it fails, it will show the error. It fails when a string that includes normal characters is given. It will pass successfully when the string only consists of numbers. Experiment a bit with try catch blocks for this. – cpb2 Jun 07 '14 at 14:37