python学习-游戏

发布时间:2019-07-16 10:09:38编辑:auto阅读(1654)

    #coding : utf-8
    #这是一个猜数字游戏
    import random
    
    guessesTaken = 0 #赋予初始值
    
    print('hello what is you name?') #输入玩家姓名
    myName = input() #从键盘接收一个值
    number = random.randint(1,20) #定义数字范围
    
    print('well,'+myName+',i am thinking of a number between 1 and 20.') 
    
    while guessesTaken < 6:    #接收键盘的次数
            print('Task a guess.') 
            guess = input()     #从键盘接收一个值
            guess = int(guess)  #定义值为int类型
            guessesTaken = guessesTaken +1 
    
    if guess < number: 
        print('your guess is too low.')#打印输出值,过低
    
    if guess > number:
        print('Your guess is too high.')#打印输出值,过高
    if guess == number:
        break
    
    if guess == number:     #猜对了输出
            guessesTaken = str(guessesTaken)
            print('good job,'+ myName + '! you guessed my number in '+ guessesTaken + 'guesses!')
    if guess != number: #不等于输入值输出
            number = str(number) 
            print('Nope. the number i was thinking of was '+ number)

关键字