有没有更简单的方法可以在python中做到这一点?

时间:2019-05-31 13:18:43

标签: python

我想尝试找到一种更简单的方法来执行此操作,但我还没有找到任何方法来完成此操作。

这是一个可以写入的菜单。我正在尝试向文本中添加个性化消息,以进一步使其具有人性化的效果,但是它看起来比它应该的更长和更长。 sandwich_t是一个询问用户是否想要三明治的问题,beverage是询问用户是否想要饮料的问题,等等。

if sandwich_t=="Yes" and beverage== "Yes" and fries== "Yes" and Ketchup== "Yes":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, and a ", beverage_selection, "drink, with ", fries_selection, "fries, with ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "Yes" and fries== "Yes" and Ketchup== "No":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, and a ", beverage_selection, "drink, with ", fries_selection, "fries, with no ketchup"
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "Yes" and fries== "No" and Ketchup== "Yes":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, and a ", beverage_selection, "drink, with no fries, and ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "No" and fries== "Yes" and Ketchup== "Yes":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, with no drink, with ", fries_selection, "fries, with ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "Yes" and fries== "No" and Ketchup== "No":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, and a ", beverage_selection, "drink, with no fries, with no ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "No" and fries== "No" and Ketchup== "No":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, with no drink, with no fries, with ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "No" and fries== "Yes" and Ketchup== "No":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, with no drink, with ", fries_selection, "fries, with no ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="Yes" and beverage== "No" and fries== "No" and Ketchup== "Yes":
    print "Cashier: In total you have so far ordered a", sandwich, "sandwich, with no drink, with no fries, with", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "Yes" and fries== "Yes" and Ketchup== "Yes":
    print "Cashier: In total so far you have no sandwich with a", beverage_selection, "drink with ", fries_selection, "fries and ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "Yes" and fries== "Yes" and Ketchup== "No":
    print "Cashier: In total so far you have no sandwich with a", beverage_selection, "drink with ", fries_selection, "fries with no ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "Yes" and fries== "No" and Ketchup== "Yes":
    print "Cashier: In total so far you have no sandwich with a", beverage_selection, "drink no fries and ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "Yes" and fries== "No" and Ketchup== "No":
    print "Cashier: In total so far you have no sandwich with a", beverage_selection, "drink, no fries with no ketchup."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "No" and fries== "Yes" and Ketchup== "Yes":
    print "Cashier: In total so far you have no sandwich with no drink, ", fries_selection, "fries with ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "No" and fries== "Yes" and Ketchup== "No":
    print "Cashier: In total so far you have no sandwich with no drink, ", fries_selection, "fries with no ketchup."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "No" and fries== "No" and Ketchup== "Yes":
    print "Cashier: In total so far you have no sandwich, no drink, no fries, with ", Ketchup_Selection, "ketchup packets."
    print "Cashier: In total it will be $", total, "alright?"
    print "You: Alright"
if sandwich_t=="No" and beverage== "No" and fries== "No" and Ketchup== "No":
    print "Chasier: You ordered nothing. Why did you come here?"

4 个答案:

答案 0 :(得分:1)

您不能多次复制您的字符串,而要对其进行部分构造:

string = 'Cashier: In total so far'

if sandwich_t == 'Yes':
    string += ' ordered a '
    string += str(sandwich)
else:
    string += ' you have no sandwich'

if beverage == "Yes":
    string += ' with a '
    string += str(beverage_selection)
    string += ' drink'
else:
    string += 'no drink, '
# ... And another fries, drinks etc etc etc

print string
print "Cashier: In total it will be $", total, "alright?"
print "You: Alright"

答案 1 :(得分:0)

循环,您听说过吗?另外,非空事物的评估结果为true。

假设番茄酱和炸薯条只是布尔值(不是字符串,是“ Yes”或“ No”),三明治则是None或名称字符串(no“ string_t”),类似的饮料:

cashier_text = "Cashier: In total you have so far ordered "
total = 0
if sandwich: # evaluates to true if string, skips if 
    cashier_text += sandwich
    cashier_text += " sandwich, and "
    total += #cost here, you don't have this in your code?
if beverage: # same as sandwich
    cashier_text += beverage
    total += #cost here, omitted in your code
if fries: # boolean
    cashier_text += " with fries"
    total += #cost here
if ketchup: #boolean
    cashier_text += " with additional ketchup"
#print cashier text here
#total and customer was always common so they should've been always here, not in if's

这比工作示例更像是pythonic伪代码-应该可以工作,但是很难看,我直接在答案编辑器中编写了它。

在您想要的其他地方添加其他句子,使句子正确无误(如我所说,我不在乎-例如,如果您仅用我的代码订购三明治,它将是“ [ ] sandwitch和”)。

答案 2 :(得分:0)

这是违反DRY (Don't Repeat Yourself)原则的一个很好的例子。您已经意识到,这里有很多不幸的重复,真是太好了!

确实有一种更轻松的方法来完成您想要的。作为奖励,它还将更清洁,更易于维护。

请注意,无论客户选择什么,您要打印的字符串中的大多数是相同的。我们真的只想替换与他们的选择相对应的正确关键词。这是您可以做到的一种方法:

if sandwich_t=="No" and beverage== "No" and fries== "No" and Ketchup== "No":
    print("Chasier: You ordered nothing. Why did you come here?")
else:
    sandwich_message = "no sandwich"
    if sandwich_t == "Yes":
        sandwich_message = "a {sw} sandwich".format(sw=sandwich)

    beverage_message = "with no drink"
    if beverage == "Yes":
        beverage_message = "and a {bs} drink".format(bs=beverage_selection)

    fries_message = "with no fries"
    if fries == "Yes":
        fries_message = "with {fs} fries".format(fs=fries_selection)

    ketchup_message = "with no ketchup"
    if Ketchup == "Yes":
        ketchup_message = "with {ks} ketchup packets".format(ks=Ketchup_Selection)

    message = "Cashier: In total you have so far ordered {sm}, {bm}, {fm}, {km}.\n" \
              "Cashier: In total it will be ${total} alright?\n" \
              "You: Alright"

    print(message.format(
        sm=sandwich_message,
        bm=beverage_message,
        fm=fries_message,
        km=ketchup_message,
        total=total
    ))

编辑: 补充说明:最好为变量名建立一个约定,并尽可能严格地遵循它。在您的示例中,您可以使用一些小写的带下划线的变量名(beverage_selectionfries_selection)!

尝试避免使用诸如Ketchup_Selection之类的名称。大写的变量名通常用于常量和类名。最好使用ketchup_selection

还请记住,Python 2是该语言的旧版本,如果您没有令人信服的理由继续使用Python 2,则应该真的使用Python 3。

答案 3 :(得分:0)

您可以使用格式字符串和列表索引来部分组合选择描述,以在“是”和“否”替代文本之间进行选择:

selection  = "Cashier: In total so far you have ordered "
selection += ["no sandwich", f"a {sandwich}"][sandwich_t=="Yes"]
selection += " with "
selection += ["no drinks",   f"a {beverage_selection}"][beverage=="Yes"]
selection += ", "
selection += ["no fries",    f"{fries_selection} fries"][fries=="Yes"]
selection += " with "
selection += ["no ketchup",  f"{Ketchup_Selection} ketchup packets"][Ketchup=="Yes"]
selection += "."

print(selection)
print("Cashier: In total it will be $", total, "alright?")
print("You: Alright")

选择食物描述的模式是:

["No-Text","Yes-Text"][state == "Yes"]

之所以可行,是因为List [True]等同于List [1],而List [False]等同于List [0]