-
猜年龄小游戏,有三点需求 1.允许用户最多尝试3次 2.每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y,就继续让其猜3次,以此往复,如果回答N或n,就退出程序 3.如何猜对了,就直接退出 目的是演练while的使用和if的使用
times = 0
count =3
while times <=3:
age = int(input('please type age:'))
if age == 25:
print('congratulations!')
break #猜对以后循环不需要再继续,直接break中断循环
elif age > 25:
print('too old,please try again')
else:
print('too small, please try again')
times =times +1
if times ==3:
choose = input('do you want to continue? Y/N')
if choose == 'Y' or choose == 'y': #y的大写小写都可以
times = 0 #重置times,重新计数,让他继续猜
elif choose == 'N' or choose == 'n':
times ==4 #循环的条件不满足,直接退出
break #注意这里需要放入break,不然会继续让我猜年龄,回到最开始的循环
else: #如果输入的不是y也不是n,就出现这样的情况
print('please type right, thank you')
第一题到了第二部分就完全不会了,根本没有想到用while循环
以下是我写的代码:
ge = 99
for n in range(3):
guess = int(input('please type the age'))
if guess == age:
print('you are right!')
break
else:
input('type y or n:')
-
小王身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小王计算他的BMI指数,并根据BMI指数: 低于18.5:过轻 too light 18.5-25:正常 normal 25-28:过重 little heavy 28-32:肥胖 overweight 高于32:严重肥胖 too fat,go exercise 用if-elif判断并打印结果
weight = float(input('please type your weight:'))
height = float(input('please type your height:'))
BMI_form = weight/(height**2)
print(BMI_form)
if BMI_form <18.5:
print('too light')
elif 18.5<= BMI_form <25:
print('normal')
elif 25<= BMI_form <28:
print('little heavy')
elif 28<= BMI_form <32:
print('overweight')
else:
print('Go exercise NOW')
这道题做出来了,但是参考了上课的笔记,没有背出来