【问题标题】:if statements with classes in PythonPython 中带有类的 if 语句
【发布时间】:2019-02-21 00:44:09
【问题描述】:

我正在制作一个小的命令提示菜单程序。用户选择一个字母选项,代码将他们带到一小组代码,为他们进行特定的计算。

我已经让菜单工作了。但是,当我为 Distributors 运行 C 时,什么也打印不出来。我应该改变什么?

def main():

    variable_to_cause_loop = "Y"

    while  variable_to_cause_loop == "Y":

        print ('A: Accounting')
        print ('B: Quick Estimate')
        print ('C: Distributors')

        User_Menu_Selection = input('Enter an option: ')

        if User_Menu_Selection == "A":
            print("Feature not available")


        elif User_Menu_Selection == "B":


            class Shoes:
                print('COMPANY MESSAGE', '\n' *5)
                print('--> Quick Estimates <--')

                def __init__(self, Model, Descpstr, price):
                    self.Model = Model
                    self.Descpstr = Descpstr
                    self.price = int(price)



            A100 = Shoes("A100", "Prada Flat Black", 100)
            A200= Shoes("A300", "Gucci Heel Black", 275)



            product_num = input('> Shoe Model(model number): ')
            product_size = input('> Shoe Size: ')
            product_qty = input('> Quantitiy: ')
            ship_zip_code = input('> Ship to Zip Code: ')




            if product_num == "A100" or product_num == "a100":
                order_calc = float(A100.price) * float(product_qty)
                print (float(order_calc))

            elif product_num == "A200" or product_num == "a200":
                order_calc = float(A200.price) * float(product_qty)
                print (float(order_calc))


            elif User_Menu_Selection == "C":
                print ("")
                print ("DISTRIBUTORS")
                print ("")
                print ("")
                print ("> Elderado's   Rep: Jack Reed   - Phone  # 1-888-562-2229 <")
                print ("> Friendly's   Rep: Roy Jack Jones   - Phone # 1-212-393-9939 <")
                print ("> Best Shoes Fort Wayne  Rep: Price Gouger   - Phone # 1-331-3832 <")
                print ("> The Rubber Maker   Rep: Crumble Crustipher   - Phone # 1-434-1919 <")




    variable_to_cause_loop = input ("For Main Menu Type Y" )

main()

【问题讨论】:

  • 您的缩进是否与问题中出现的完全相同(即“C”的 elif 是 product_num ifs 的对等项,而不是其他 User_Menu_Selection ifs)还是只是一种格式粘贴时出错?

标签: python-3.x oop if-statement


【解决方案1】:

正如@craig-meier 的评论中提到的,取消缩进elif User_Menu_Selection == "C":,使其与“A”和“B”语句一致。

此外,您使用的是input,因此您必须在"A" 中输入引号。您很可能想改用 raw_input,这样您就可以只输入 A 而不带引号。

【讨论】:

  • inputraw_input 的事情仅适用于 python 2.x。在 python 3 中,原来的 input 被删除,raw_input 被重命名为 input
  • 我错过了指定 Python 版本的问题的标签。谢谢指正。
  • @CraigMeier 在您的经验中,raw_input 的优势是什么?
  • @CoolJack1001 raw_input 在 python 2.x 中的作用与 input 在 python 3.x 中的作用相同(即你想要的)。在 python 2.x 中,input(...) 做了 python 3.x 会做的 eval(input(...)) (这很少是一个好主意——只要问小 Bobby Tables 当你执行未经处理的输入时会发生什么)。
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2018-10-07
  • 2021-02-11
相关资源
最近更新 更多