python一个小程序:猜数字

Posted Wolf_Dreams

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python一个小程序:猜数字相关的知识,希望对你有一定的参考价值。

猜数字游戏程序运行示例:


 

I am thinking of a number between 1 and 20.
Take a guess.
8
Your guess is too low.
Take a guess.
10
Your guess is too low.
Take a guess.
15
Good job!You guessed my number in 3 guesses!

 

猜数字游戏具体代码,并保存为guess_number.py:

#-*-conding: utf8-*-
############################################
#author:wolf_dreams
#time:2018-11-12
#blog:https://www.cnblogs.com/Wolf-Dreams/
############################################
import random
secretNumber = random.randint(1,20)
print("I am thinking of a number between 1 and 20.")

#Ask the player to guess 6 times.
for guessesTaken in range(1,20):
    print("Take a guess.")
    guess = int(input())

    if guess < secretNumber:
        print("Your guess is too low.")
    elif guess > secretNumber:
        print("Your guess is too high.")
    else:
        break
if guess == secretNumber:
    print("Good job!You guessed my number in " + str(guessesTaken) + " guesses!")
else:
    print("Nope.The number I was thinking of was " + str(secretNumber))

 

以上是关于python一个小程序:猜数字的主要内容,如果未能解决你的问题,请参考以下文章

python小练习简单的猜数字游戏

python-猜数字小练习

猜数字小程序的实现

Python基础入门-实现猜数字小游戏

python之猜数字

运用python写一个猜数字游戏,学自小甲鱼老师