首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中有没有更简单的方法来做到这一点?

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

Stack Overflow用户
提问于 2019-05-31 21:18:43
回答 4查看 102关注 0票数 0

我想试着找到一种更简单的方法来做到这一点,但我还没有找到任何方法。

这是一个可以写入的菜单。我正在尝试在文本中添加个性化的消息,以进一步使其更像人类,但是它似乎非常长,而且比它应该的更长。sandwich_t是询问用户是否想要三明治的问题,beverage是询问用户是否想要饮料的问题,等等。

代码语言:javascript
复制
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?"
EN

回答 4

Stack Overflow用户

发布于 2019-05-31 21:25:48

您不能将字符串复制几十次,但可以逐个部分地构造它:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-05-31 21:36:58

Loops,你听说过它们吗?此外,非空的事物的计算结果为真。

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

代码语言:javascript
复制
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伪代码,而不是工作示例-它应该可以工作,但它会很难看,我直接在answer编辑器中编写了它。

在你想要的任何地方添加else,让句子变得有意义,这样如果没有什么东西就有意义了(就像我说的,我并不关心它--例如,如果你只用我的代码点三明治,它将是"name sandwitch,and")。

票数 1
EN

Stack Overflow用户

发布于 2019-05-31 21:43:17

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

确实有一种更简单的方法来完成你想要的东西。作为一个额外的好处,它也将变得更干净和更易维护。

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

代码语言:javascript
复制
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

还要记住,Python2是该语言的遗留版本,如果没有令人信服的理由继续使用Python2,那么您真的应该迁移到Python3。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56395871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档