Python 如何提高效率?
有哪些方法可以使代码更干净、更高效? 我是python新手,我不想养成坏习惯! 谢谢你的帮助Python 如何提高效率?,python,Python,有哪些方法可以使代码更干净、更高效? 我是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 ca
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))
为什么第一行缩进?您也不需要将服务器名称放入变量中—它会添加一行不必要的内容。只需将其包含在服务器名称(Will)所在的字符串中即可。你也可以试着把饮料的价格放在字典里。另外,你为什么要这样做:
str(酒水价格)+str(食品价格)
为什么要将drinkPrice
和foodPrice
转换成字符串,而它们应该保留为数字?这只会将它们作为字符串连接在一起,并会导致逻辑错误。假设饮料的价格是5,食品的价格是4,你的程序会把总价格变成54,而它应该是9
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))
while循环的要点是,你可以一直问,直到你得到一个你想要的有效答案
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))
这个问题比堆栈溢出更适合。如果您有功能代码,那么这个问题可能更适合,但请阅读以检查它是否在主题上。我认为这不适合代码审查,因为代码中存在错误。仅供参考:
str(drinkPrice)+str(foodPrice)
将连接字符串,它不会通过添加值来获得总价。@WillPatterson会在您的问题中解决它。在大规模编码时,将服务器名称放入变量是一种良好的做法,这样您就可以知道事情的位置,并可以在需要时更改它,而它们都在一个位置。当有更好的解决方案时,为什么要推荐2D阵列中的饮料和价格。OP要求更好的编码实践,我个人认为你的3点中的最后一点是可以接受的。
x = 5
y = 6
print(x+y)
>> 11
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))