Guess the number

猜字游戏<Invent with Python>

1.你只有六次机会去猜字电脑给出得1--20之间的随机数,电脑将根据你猜的数字来告诉你高还是低.

2.这个游戏中将使用python中得random,loops, raw_input .这个游戏代码中,将学习如何将不同的类型转换

3.for example


Hello! What is your name?
Albert
Well, Albert, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too high.
Take a guess.
2
Your guess is too low.
Take a guess.
4

Good job, Albert! You guessed my number in 3 guesses!


# This is a guess the number game.
#@author Invent with python

import random

guessesTaken = 0

print('Hello! what is your name?')
name = raw_input()

number= random.randint(1,20)
print('Well, '+name+' , I am thinking of a number  between 1 and 20.')

while guessesTaken < 6:
    print('Take a guess.')
    guess = input()
    guess = int(guess)

    guessesTaken +=1

    if guess < number:
        print('Your guess is too low')

    if guess > number:
        print('Your guess is too hight.')

    if guess == number:
        break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job, '+ name + ' ! You guessed my number in '+guessesTaken+" guesses")

if guess !=number:
    number = str(number)
    print('Nope. The number I was  thinking of was '+ number) 

这段代码没有经过严格的异常控制



你可能感兴趣的:(Guess the number)