有哪些方法可以使此代码更清晰,更高效? 我是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))
答案 0 :(得分:0)
为什么第一行缩进?您也不需要将服务器的名称放在变量中 - 它会添加不必要的行。只需将其包含在服务器名称(Will)所在的字符串中。您也可以尝试将饮料及其价格放入字典中。另外你为什么要这样做:str(drinkPrice) + str(foodPrice)
为什么您将drinkPrice
和foodPrice
转换为字符串,何时应该保留为数字?这只会将它们作为字符串连接在一起,并会导致逻辑错误。所以说饮料的价格是5,食品价格是4,你的程序将使总价格变为54,当它应该是9。