3 回答
TA贡献1796条经验 获得超7个赞
您永远不会重读“ day”,因此“ day of day”始终是正确的,并且会无休止地执行。
def weekday()
day = input('Enter day abbreviation ' )
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':'Sunday'}
while day in days:
print(days.get(day))
day = input('Enter day abbreviation ' )
TA贡献1946条经验 获得超4个赞
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':'Sunday'}
while True:
day = input('Enter day abbreviation ' )
if day in days:
print (days[day])
else:
break
输出:
$ python3 so.py
Enter day abbreviation Mo
Monday
Enter day abbreviation Tu
Tuesday
Enter day abbreviation we
Wednesday
Enter day abbreviation foo
另一种使用方式dict.get:
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':'Sunday'}
obj = object() #returns a unique object
day = input('Enter day abbreviation ' )
while days.get(day,obj) != obj:
print (days[day])
day = input('Enter day abbreviation ' )
TA贡献1784条经验 获得超8个赞
您想在每次迭代中再次获取输入:
while True:
day = input('Enter day abbreviation ' )
acquired_day = days.get(day)
if acquired_day is None: break
print(acquired_day)
添加回答
举报