用python编写猜数字游戏

版权声明:严禁转载 https://blog.csdn.net/qq_38452951/article/details/84178788
import random--导入包random
number=random.randint(1,100)--定义一个1到100的随机数
is_done=False--刚开始还没猜
count=0--定义猜测次数
while not is_done:
    guess=int(input('please input a number between 1 and 100:'))--输入你的猜测数
    if guess==number:--猜对了
        is_done=True
    elif guess>number:--猜大了
        print('it’s  too big,try again')
    elif guess<number:--猜小了
        print('it’s  too samll,try again')
        count+=1--每猜一次加1
        print('congratuation to you,you guess{} times,finally get it,answer is{}'.format(count,number))--显示你猜对了,并且显示猜测次数

总结:首先在python中非常注重大小写,谨防把False写成了false,True写成了true.用到必要的函数时你得先导包,还有最后一行有中括号,后面接了.format(count,number),显然括号里是前面中括号里的参数

猜你喜欢

转载自blog.csdn.net/qq_38452951/article/details/84178788