Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Using one line in Try in python

$
0
0

I'm studying CS50p and the tutor says it's best practice to have as few lines as possible, ideally one, in a Try, but I really struggle to do this.

How could I write the following code so that I only try the input?

while True:    try:        # How to have just 1 line in try??        order = input("Order: ").title().strip()        if order in menu:            price += menu[order]            print(f"Total: ${price}")    except (EOFError, KeyError):        break

Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>