输入不按计划工作,无法修复(python)

时间:2014-06-03 08:52:19

标签: python

我正在为学校制作比萨饼操作员计划。什么是基本上是要求客户的名字,询问你是否想要提货或交货等。我遇到的问题是,当我没有在输入中输入任何东西时,它会给出错误,让你输入它但随后会自动停止该程序。我希望它重复一遍。当我点击1,2或3时,我在get_user_info中有一个错误它什么也没做,只是进入一个我似乎无法解决的无限循环。我希望你们能提前帮助我,非常感谢你们。

此致 乔纳森

我的代码

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 个答案:

答案 0 :(得分:0)

只要客户名称为空,您就想循环:

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")

另外,我会重新考虑使用全局变量。使用全局变量的程序不能很好地扩展。

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)

以后的程序:

 customer_name = get_customer_name()

答案 1 :(得分:0)

第一个问题: 我会改变

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

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

另一个问题......快速建议:你确定要读取user_input并使用get_user_info作为变量吗?只需使用user_input,而不是函数的名称:) ^^

编辑以免污染编辑地点:

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()

基本上你循环直到user_input与3不同,你的解决方案的(主要)问题是你使用伪造变量(get_user_info)来执行检查以让循环结束,但是你设置了另一个( user_input);)

答案 2 :(得分:0)

1。使用raw_input()

2. 使用对象/类。它将使您的程序更具可读性/可调试性/组织性。

3。您的部分if语句可以重写。

4. 为了便于阅读,请将全局变量设为大写。 (实际上不确定这是否在PEP-8中,但我总是这样做)

实施例

这是我做的。它很脏但是有效。

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))

输出

    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?

您可能希望将我的代码段用作制作包含所有比萨饼及其个别奖品的类的示例,并在其中实现!