猜数字游戏

猜数字游戏

猜数字小游戏,在多少次以内猜中数字。游戏规则如下,随机给出【1,100】之间的数字,玩家有6次猜数的机会,猜中退出游戏;猜错重试,超过6次则游戏失败。

import random

guessesTaken = 0 # 猜测次数
print("Hello! Who are you?")
myName = input()
number = random.randint(1, 100)
print('Well, ' + myName + ', I am thinking of a number between 1 and 100.')
while guessesTaken < 6:
    print('Take a guess.')
    guess = int(input()) 
    guessesTaken += 1
    if guess < number:
        print('Your guess is too low.')
    elif guess > number:
        print('Your guess is too high.')
	else:
        break
        
if guess == number:
    guessTaken = str(guessesTaken)
    print('Good job,' + myName + '! Your guessed my number in ' + guessesTaken + ' guesses!')
else:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number)

你可能感兴趣的:(python,游戏,python,前端)