if_else语句
同时被3或7整除作业:
num=input('请输入一个数:')
if int(num)%3==0 and int(num)%7==0:
print('它可以被3、7同时整除')
else:
print('它不可以被3、7同时整除')
判断闰年作业:
# 判断闰年: 四年一闰,百年不闰,四百年再闰
year = int(input('请输入一个年份:'))
if (year%4==0 and year%100!=0) or (year%400==0):
print('这是闰年:',year)
else:
print('这不是闰年')
用秒换算时间
s=int(input('请输入秒数:'))
hour = s//3600
minute = s%3600//60
second =s%60
print("共用%d时,%d分,%d秒"%(hour,minute,second))
判断BMI值,条件是:体重(kg)/身高(m)的平方值,在18.5 至 24.9 之间正常
weight=float(input('请输入体重(kg):'))
height=float(input('请输入身高(m):'))
BMI=weight/height**2
if BMI>24.9:
print('您太肥胖了')
elif BMI<18.5:
print('您太瘦了')
else:
print('您体型正常')
pass的用途
age =int(input('请输入你的年龄'))
if age>18:
pass # 相当于占位,没有实际意义,但可以确保程序继续执行下去
print('hello')
石头剪刀布练习
import random
player=int(input('请输入 (0)剪刀 (1)石头 (2)布:'))
print('您输入的是',player)
computer = random.randint(0,2) #表示取[0,2]之间的整数
print('电脑输入的是',computer)
if (player ==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1):
print('恭喜您,您赢了!')
elif player==computer:
print('平局')
else:
print('对不起,您输了!')
浙公网安备 33010602011771号