2017-08-03 12 views
-5

このコードをよりクリーンに、より効率的にするための方法は何ですか? 私はPythonには新しく、悪い習慣を始めたいとは思っていません! 助けてくれてありがとう!これをより効率的にする方法は?

store = input('Name of store: ') 
food = input('Type of food served: ') 
serverName = 'Will' 
drinkPrice = '' 
foodPrice = '' 
drink = input('Hello! My name is {0} and I will be your server today! What can I get you to drink?: '.format(serverName)) 
if drink == "Water": 
    drinkPrice = 1 
else : 
    if drink == "Coke": 
    drinkPrice = 2 
else : 
    if drink == "Beer": 
    drinkPrice = 5 
else : 
    print("The item you are trying to order is not on the menu!") 
drink = input("What else would you like to drink?:") 
food = input('What will you be ordering tonight?: ') 
if food == "Steak": 
    foodPrice = 25 
else : 
    if food == "Pizza": 
    foodPrice = 10 
else : 
    if food == "Salad": 
    foodPrice = 5 
else : 
    print("The item that you are trying to order is not on the menu!") 
totalPrice = str(drinkPrice) + str(foodPrice) 
print('Thanks for eating at {0} and ordering {1} ! Server name: {2} Price total: {3}'.format(store, food, serverName, totalPrice)) 
+9

これはあなたのコードがどのように見えるかです。 – CoryKramer

+0

コードが機能している場合、この質問はhttps://codereview.stackexchange.com/より適切ですが、[faq](https://codereview.stackexchange.com/help/dont-ask)を読んでそこに話題があります。 – EdChum

+0

コードにバグがあるので、これはコードレビューには合わないと思います。 – MooingRawr

答えて

0

なぜ最初の行がインデントされていますか?サーバーの名前を変数に入れる必要もなく、不要な行が追加されます。サーバの名前(ウィル)が入っている文字列にそれを含めてください。また、価格でドリンクを辞書に入れてみることもできます。さらに、なぜこれをやったのですか:str(drinkPrice) + str(foodPrice) drinkPricefoodPriceを文字列に変換したのはなぜですか?これは文字列として一緒に結合するだけで、論理エラーが発生します。だから、飲み物の価格は5だったと食料価格は4だったと言う、あなたのプログラムは、それが私のコメントのポイントをまとめると9

+0

サーバーの名前を変数に入れることは、大規模なコーディングでは良い習慣であるため、必要に応じて、すべてが1つの場所にある間に、状況を把握して変更することができます。より良い解決策がある場合、2Dアレイで飲み物や価格をお勧めします。 OPはより良いコーディング慣行を求めており、個人的にはあなたの3のうちの最後のポイントが受け入れられると思います。 – MooingRawr

1
store = input('Name of store: ') 
food = input('Type of food served: ') 
serverName = 'Will' 
drinkPrice = ''    #If it's a price (number why not set it to an integer example : drinkPrice = 0. '' refers to an empty string. 
foodPrice = '' 
drink = input('Hello! My name is {0} and I will be your server today! What can I get you to drink?: '.format(serverName)) 
if drink == "Water": 
    drinkPrice = 1 
else :      # in python if-else are written as elif <condition>: so it would be elif drink == "Coke": 
    if drink == "Coke": 
    drinkPrice = 2 
else :      # will never reach here 
    if drink == "Beer": 
    drinkPrice = 5 
else :      #will never reach here 
    print("The item you are trying to order is not on the menu!") 
drink = input("What else would you like to drink?:") # you aren't looping here so you might want a loop. 
food = input('What will you be ordering tonight?: ') 
if food == "Steak": 
    foodPrice = 25 
else :      #same issue as the drink. 
    if food == "Pizza": 
    foodPrice = 10 
else : 
    if food == "Salad": 
    foodPrice = 5 
else : 
    print("The item that you are trying to order is not on the menu!") 
totalPrice = str(drinkPrice) + str(foodPrice)   #Python allows '+' to be used on strings as a form of concatination (combining strings). 
                 #You want int(drinkPrice) + int(foodPrice) or just do drinkPrice + foodPrice 
                 #since you set those value to 1, 2, 5, etc.. 
print('Thanks for eating at {0} and ordering {1} ! Server name: {2} Price total: {3}'.format(store, food, serverName, totalPrice)) 

あるべきとき、54に合計金額になるだろう:

文が次のように書かれている場合は、次の

if <condition>: 
    #do something 
elif <condition>: 
    #do something 
else: 
    #default if the top two didn't pass 

あなたは、ループ上に読む必要があるが、私はあなたが欲しいかもしれない1がwhileループだと思う:

while <condition>: 
    #loops until the condition is False 

whileループのポイントは、有効な回答が得られるまで尋ねることができます。 See this link for more details


Pythonには、このような文字列としてオブジェクト+が非数で使用することができます:

x = "5" 
y = "6" 
print(x+y) 
>> 56 

あなたの変数が数値であることを確認する必要があります。

x = 5 
y = 6 
print(x+y) 
>> 11 

"5" であります5と同じではなく、最初は5の文字列表現であり、後者は数値5です。これは "0"になる空の文字列です。

コードが機能しないため、マイクロ最適化について心配する必要はありません。この質問はスタックオーバーフローより[コードレビュー](http://codereview.stackexchange.com)に適してい

store = input('Name of store: ') 
food = input('Type of food served: ') 
serverName = 'Will' 
drinkPrice = 0 
foodPrice = 0 
drink = input('Hello! My name is {0} and I will be your server today! What can I get you to drink?: '.format(serverName)) 
while drinkPrice == 0: 
    if drink == "Water": 
     drinkPrice = 1 
    elif drink == "Coke": 
     drinkPrice = 2 
    elif drink == "Beer": 
     drinkPrice = 5 
    else : 
     print("The item you are trying to order is not on the menu!") 
     drink = input("What else would you like to drink?:") 

food = input('What will you be ordering tonight?: ')  
while foodPrice == 0: 
    if food == "Steak": 
     foodPrice = 25 
    elif food == "Pizza": 
     foodPrice = 10 
    elif food == "Salad": 
     foodPrice = 5 
    else : 
     print("The item that you are trying to order is not on the menu!") 
     food = input("What else would you like to eat?:") 

totalPrice = drinkPrice + foodPrice 
print('Thanks for eating at {0} and ordering {1} ! Server name: {2} Price total: {3}'.format(store, food, serverName, totalPrice)) 
関連する問題