- int 整型
- float 浮点型
- bool 布尔类型 0 1
- str 字符串
你知道为什么布尔类型(bool)的 True 和 False 分别用 1 和 0 来代替吗?
针对视频中小甲鱼提到的小漏洞,再次改进我们的小游戏:当用户输入错误类型的时候,及时提醒用户重新输入,防止程序崩溃。
import random
times = 3
secret = random.randint(1,10)
print('------------------我爱鱼C工作室------------------')
guess = 0
print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
while (guess != secret) and (times > 0):
temp = input()
if temp.isdigit():
guess = int(temp)
if guess == secret:
print("我草,你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没有奖励!")
else:
if guess > secret:
print("哥,大了大了~~~")
else:
print("嘿,小了,小了~~~")
if times > 1:
print("再试一次吧:", end='')
else:
print("机会用光咯T_T")
else:
print("抱歉,您的输入有误,请输入一个整数:", end='')
times = times - 1 # 用户每输入一次,可用机会就-1
print("游戏结束,不玩啦^_^")
写一个程序,判断给定年份是否为闰年。(注意:请使用已学过的 BIF 进行灵活运用
这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年。
year = int(input("请输入一个年份:"))
if year % 4 == 0 and year % 100 != 0:
print("这一年是闰年!")
elif year % 400 == 0:
print("这一年是闰年!")
else:
print("这不是闰年!")
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if year/400 == int(year/400):
print(temp + ' 是闰年!')
else:
if (year/4 == int(year/4)) and (year/100 != int(year/100)):
print(temp + ' 是闰年!')
else:
print(temp + ' 不是闰年!')
s.isdigit() 所有字符都是数字,为真返回True,为假返回False。