2017-08-03 106 views
-5

有什麼方法可以使代碼更清潔,更高效? 我是新來的蟒蛇,我不想開始壞習慣! 感謝您的幫助!如何提高效率?

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

這個問題比堆棧溢出更適合於[代碼審查(http://codereview.stackexchange.com)。 – CoryKramer

+0

如果你有功能代碼,那麼這個問題可能更適合https://codereview.stackexchange.com/,但閱讀[常見問題](https://codereview.stackexchange.com/help/dont-ask)來檢查是否這是關於話題 – EdChum

+0

我不認爲這會適合Code Review,因爲代碼中存在錯誤。 – MooingRawr

回答

0

爲什麼第一行縮進?你也不需要把服務器的名字放在一個變量中 - 它增加了一個不必要的行。只需將其包含在服務器名稱(Will)所在的字符串中即可。您還可以嘗試將其價格放在字典中。此外,爲什麼你這樣做:str(drinkPrice) + str(foodPrice) 爲什麼你將drinkPricefoodPrice轉換爲字符串時,他們應該保持爲數字?這隻會將它們作爲字符串連接在一起,並會導致邏輯錯誤。所以說的飲料價格爲5,食品價格是4,你的程序會使得總價格爲54,當它應該是9

+0

將服務器的名稱放在變量中是一種很好的做法,因爲它可以在更大的範圍內進行編碼,所以您可以知道事物是什麼,並且可以在需要時更改它們,而它們都在同一個位置。當有更好的解決方案時,您爲什麼會推薦二維陣列中的飲料和價格。 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)) 

要總結我的評論點:

如果語句寫成如下:

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

您需要在循環讀了,但我想你可能想要的是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.

你的代碼不起作用,所以你不應該擔心微觀優化。

這是你的代碼應該是什麼樣子:

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