practice_1(猜数字小游戏)

10 < cost < 50 的等价表达式
cost = 40
# (10<cost) and (cost>50)
10 < cost <50
True
使用int()将小数转换成整数,结果是向上取整还是向下取整
print(int(3.4))    # 向下取整
3
写一个程序,判断给定年份是否为闰年
  • 闰年的定义:能够被4整除的年份就叫闰年
6 % 4
2
year = input("请输入年份:")
# 判断用户输入的是否为数字

if year.isdigit():
    year = int(year)
    
    if year % 4 == 0:
        print(str(year) + "是闰年")    
    else:
        print(str(year) + "不是闰年")
else:
    print("请输入年份!!")
请输入年份:2000
2000是闰年
"1" + 1
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-8-ec358fc6499a> in <module>
----> 1 "1" + 1


TypeError: can only concatenate str (not "int") to str
给用户三次机会,猜想我们程序生成的一个数字A,每次用户猜想过后会提示数字是否正确以及用户输入的数字是大于还是小于A,当机会用尽后提示用户已经输掉了游戏
import random

secert = random.randint(1,100)   # 计算机生成一个随机数

times = 3   # 初始化用户的次数是3

while times:
    num = input("请输入数字: ")
    if num.isdigit():
        temp = int(num)
        if temp == secert:
            print("你猜对了!!")
            break
        elif temp < secert:
            print("你的数字太小了")
            times = times - 1
        else:
            print("你的数字太大了")
            times = times - 1
            
    else:
        print("请输入数字!!")
        
print("你的机会用完了")
请输入数字: 78
你的数字太大了
请输入数字: 70
你的数字太小了
请输入数字: 76
你的数字太大了
你的机会用完了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值