congratulations!please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}please enter the num:2Sorry, we don't have this gift.Please reselectplease choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}please enter the num:1Congratulations, you have received this gift,the gift is doll
复制代码
2.2 for循环
其实和while差不多,好处就是他不会溢出,知道位置便可以打印了
这个循环函数可以做一些效果出来
# 注意,这里有几个循环终止的时候就需要几个break
real_age = 18
while True:
age = int(input("please enter the age"))
if age == 18:
print('congratulations!')
while True:
prize_dict = {0:'toy_car',1:'doll',2:'puzzle'}
print(f'please choose one of these gifts: {prize_dict}')
prize = int(input('please enter the num:'))
if prize == 1:
print(f'Congratulations, you have received this gift,the gift is {prize_dict[1]}')
break
else:
print("Sorry, we don't have this gift.Please reselect")
break
else:
print("sorry,guess wrong")
if age >18:
print("sorry,guess older")
else:
print("sorry,guess younger")
复制代码
please enter the age23
sorry,guess wrong
sorry,guess older
please enter the age15
sorry,guess wrong
sorry,guess younger
please enter the age18
congratulations!
please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}
please enter the num:2
Sorry, we don't have this gift.Please reselect
please choose one of these gifts: {0: 'toy_car', 1: 'doll', 2: 'puzzle'}
please enter the num:1
Congratulations, you have received this gift,the gift is doll