python猜数字游戏

生成一个随机数,你有三次机会输入1到20之间的一个数字,每输入一次我都会提醒你这个数字是大了还是小了。
猜对了会鼓励你一下,游戏结束。三次失败后游戏也会结束

#! /usr/bin/env python              
# -*- coding: utf-8 -*
import random                       #引入随机数模块
print('start game')
print('you just have three chance to attempt')
randnum=random.randint(1,20)         #生成1到20之间的一个随机数
n=0
while n<3:
    temp=input('please input a number between 1 to 20:')
    num=int(temp)
    if num>20:
        print('error input,input beyond scope')
    else:
        if num<1:
            print('error input,input beyond scope')
        else:
            if num==randnum:
                m=1
                break
            else:
                if num>randnum:
                    print('input is too big')
                    m=0
                else:
                    print('input is too small')
                    m=0
    n=n+1
    chance_num=3-n
    print ('you just have %d chance'%chance_num)
if m==1:
    print('you are strong,give you a 么么哒')
else:
    if m==0:
        print('you are too weak,再接再厉')

附运行结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_40303160/article/details/81413250