2017-08-03 108 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))