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))

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

时间: 2024-07-30 15:46:36

python一个小程序:猜数字的相关文章

python 一个小程序开启博客之旅:

#_*_ coding:utf-8 _*_ print_num = input ('which loop do you want it to be printed out?') count = 0 while count < 100000: if count == print_num: print 'there you got the number:', count choice = raw_input ('Do you want to continue the loop?(y/n)') if

Python 练习册,每天一个小程序

Python 练习册,每天一个小程序 说明: Python 练习册,每天一个小程序.注:将 Python 换成其他语言,大多数题目也适用 不会出现诸如「打印九九乘法表」.「打印水仙花」之类的题目 点此链接,会看到每个题目的代码, 欢迎大家 Pull Request 出题目,贴代码(Gist.Blog皆可):-) 本文本文由@史江歌([email protected] QQ:499065469)根据互联网资料收集整理而成,感谢互联网,感谢各位的分享.鸣谢!本文会不断更新. Talk is chea

Python练习册,每天一个小程序

Python练习册,每天一个小程序 精选评论关注该公众号可参与评论 写评论 加载中 以上评论由公众帐号筛选后显示 Python练习册,每天一个小程序 提交 我的评论 已评论 Python练习册,每天一个小程序 2014-12-15 程序猿 说明: ●Python 练习册,每天一个小程序.注:将 Python 换成其他语言,大多数题目也试用 ●不会出现诸如「打印九九乘法表」.「打印水仙花」之类的题目 ●欢迎大家 Pull Request 出题目,贴代码(Gist.Blog皆可):-) ●访问链接h

Python 练习冊,每天一个小程序

Python 练习冊,每天一个小程序 说明: Github 原文地址: 点击打开链接 Python 练习冊.每天一个小程序.注:将 Python 换成其它语言,大多数题目也试用 不会出现诸如「打印九九乘法表」.「打印水仙花」之类的题目 欢迎大家 Pull Request 出题目.贴代码(Gist.Blog皆可):-) Talk is cheap. Show me the code.--Linus Torvalds 第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字.类似于微

一个python爬虫小程序

起因 深夜忽然想下载一点电子书来扩充一下kindle,就想起来python学得太浅,什么“装饰器”啊.“多线程”啊都没有学到. 想到廖雪峰大神的python教程很经典.很著名.就想找找有木有pdf版的下载,结果居然没找到!!CSDN有个不完整的还骗走了我一个积分!!尼玛!! 怒了,准备写个程序直接去爬廖雪峰的教程,然后再html转成电子书. 过程 过程很有趣呢,用浅薄的python知识,写python程序,去爬python教程,来学习python.想想有点小激动…… 果然python很是方便,5

python写的第一个简单小游戏-猜数字

1 #Filename:game1.py 2 3 guess=10 4 running=True 5 while running: 6 try: 7 answer=int(raw_input('Guess what i think:')) 8 except: 9 print 'Please input interga\n' 10 continue 11 12 if answer<guess: 13 print 'Your answer is too small\n' 14 continue 15

Python实现简单的猜数字游戏

Python实现简单的猜数字游戏,具体如下: 随机生成一个1-10之间的数字,让用户来猜,当猜错时,会提示猜的数字是大还是小了,直到用户猜对为止. import random secret = random.randint(1,10) #print(secret) print('------猜数字游戏!-----') guess = 0 while guess != secret: temp = input('猜数字游戏开始,请输入数字:') guess = int(temp) if guess

java web 程序---猜数字游戏

思路:1.第一个是随机产生的数字,告诉我们去猜  cai.jsp 2.第二个是一个form表单,提交按钮后,将连接到验证页面 test1.jsp 3.第三个是比较猜的数和随机数.对了,提示再玩一次,不对则继续猜.用一个超链接 test2.jsp 老师的思路越来越难搞了.怎么写啊,用到hashMap时候 cai.jsp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <%@ page lang

关于链表的一个小程序

关于链表的一个小程序: /**************************链表*****************************//* 具备功能 *//* 链表按元素位置插入 *//* 链表按元素位置删除 *//* 链表全表遍历 *//* 链表整表创建(头插法) *//* 链表整表创建(尾插法) *//* 链表整表删除 *//**************************链表*****************************/ #include<stdio.h>#in