需求1:猜年龄,错误3次,终止游戏
num = 3
age = 18
while num > 0:
age1 = int(input('请输入年龄:'))
if age1 == age:
print('恭喜猜对了')
break
elif age1 > age:
print('大了')
else:
print('小了')
num -= 1
print(f'你还剩下{num}次机会')
需求2:猜年龄,错误3次,询问是否在此游戏。输入Y/N确认
需求2代码如下:
num = 3
age = 18
while num > 0:
age1 = int(input('请输入年龄:'))
if age1 == age:
print('恭喜猜对了')
break
elif age1 > age:
print('大了')
else:
print('小了')
num -= 1
print(f'你还剩下{num}次机会')
if num == 0:
test = input('请问还玩吗?Y/N')
if test == 'Y' or 'y':
num = 3
continue
else:
break