【发布时间】:2015-11-11 19:17:28
【问题描述】:
f=open('food.dat','w+')
rates=0
rate=0
print"Menu is:"
print""" 1.Starters
2.Main Courses
3.Snacks
4.Drinks
5.Desserts"""
hotel_food1={1:'Starters',
2:'Main Courses',
3:'Snacks',
4:'Drinks',
5:'Desserts'}
while True:
food=input("Enter the food type:")
if(hotel_food1.has_key(food)==1):
print"Menu is:"
print"""1.Salmon Devilled Eggs
2.Baked Parika Cheese
3.Ricotta and Parmesan Fritters
4.Traditional Welsh Cawl
5.Summer Ratatouille Salad"""
hotel_starter={1:'Salmon Devilled Eggs',
2:'Baked Parika Cheese',
3:'Ricotta and Parmesan Fritters',
4:'Traditional Welsh Cawl',
5:'Summer Ratatouille Salad'}
fd=input("ENter the food type")
if(hotel_starter.has_key(fd)==1):
rate=rate+234
elif(hotel_starter.has_key(fd)==2):
rate=rate+345
elif(hotel_starter.has_key(fd)==3):
rate=rate+200
elif(hotel_starter.has_key(fd)==4):
rate=rate+110
elif(hotel_starter.has_key(fd)==5):
rate=rate+334
ch=raw_input("Do you want to continue? yes/no??")
if(ch=='no'):
break
rates=rates+rate
f.write(str(rates))
f.seek(0,0)
x=f.read()
print x
f.close()
您好,在上面的程序中,当我输入食物为 1 和 fd 为 1 时,我必须打印值 234。但是当我运行程序时,我得到的值为 0。缩进有问题吗?字典是否正确执行?请帮忙!谢谢!
【问题讨论】:
-
使用哪个版本的python?您确定输入将
int传递给fd? -
我使用的是 python 2.7 版本。
-
我得到的打印值为 234:i.imgur.com/7uMSdCh.png
-
你能告诉我缩进,因为它不适用于我的。
-
我已经复制了您的代码完全,正如您提供的那样,并通过控制台/命令提示符运行它。没有缩进错误。
标签: python file if-statement dictionary output