码迷,mamicode.com
首页 > 微信 > 详细

python一个小程序:猜数字

时间:2018-11-13 00:00:00      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:break   dream   you   secret   for   else   class   http   thinking   

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


 

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一个小程序:猜数字

标签:break   dream   you   secret   for   else   class   http   thinking   

原文地址:https://www.cnblogs.com/Wolf-Dreams/p/9949442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!