简体   繁体   中英

Is there an easier way to do this in python?

I want to try to find an easier way to do this and I haven't found any way to do it.

This is meant to be a menu that can be written in. I am trying to add personalized messages to the text to further make it human-like, however it seems very long and longer than it should be. sandwich_t is a question asking the user if they want a sandwich, beverage is asking the user if they want a beverage, etc.

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

You can not to copy your string dozens of times, but construct it part-by-part:

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"

Loops, have you heard of them? Also, non-empty things evaluate to true.

Assuming ketchup, and fries are just booleans ( not strings with "Yes" or "No") and sandwich is either None or a string with name (no "string_t"), similarly beverage:

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

This is more of a pythonic pseudocode than working example - it should work but it will be ugly, I wrote it directly in answer editor.

Add else wherever you want, make the sentence so it makes sense if something is not there (as I said, I didn't care about it - eg if you order a sandwich only with my code it will be "[name] sandwitch, and").

This is a great example of violating the DRY (Don't Repeat Yourself) principle. You've alreay recognized that there's a lot of unfortunate repetition here, so great job!

There is indeed a much easier way to accomplish what you want. As a bonus it will also be cleaner and more maintainable.

Notice that most of the string you're printing is the same, no matter what the customer selected. We really just want to substitute the right keyphrases that correspond to their choices. Here's one way you could do it:

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

Edit: One additional note: It's best to establish a convention for variable names and stick to it as closely as you can. In your example, you have some lowercase, underscored variable names ( beverage_selection , fries_selection ) which are good!

Try to avoid names like Ketchup_Selection . Capitalized variable names are often used for constants and class names. It would be better to use ketchup_selection .

Also keep in mind that Python 2 is the legacy version of the language, and you should really move to Python 3 if you don't have a compelling reason to keep using Python 2.

You could assemble the selection description part by part using format strings and a list index to select between "Yes" and "No" alternative texts:

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

The pattern for selection of a food item descriptions is:

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

This works because List[True] is equivalent to List[1] and List[False] is equivalent to List[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM