下边是一个猜数字的小游戏:

几乎所有语言都这样做的……

here we go!

 import random
secret = random.randint(1,99)
guess = 0
tries = 0
print "AHOY! I'm the Dread Pirate Roberts,and I have a secret!"
print "It is a number from 1 to 99. I'll give you 6 tries."
while guess != secret and tries < 6:
guess = input("What's yer guess?")
if guess < secret:
print "Too low ,ye scurvy dog!"
elif guess > secret:
print "Too high,landlubber!" tries = tries + 1 if guess == secret:
print "Avast! ye got it! Found my secret, ye did!"
else:
print "No more guesses! Better luck next time, matey!"
print "The secret number was",secret

这是运行效果(猜对了):

>>> ================================ RESTART ================================
>>>
AHOY! I'm the Dread Pirate Roberts,and I have a secret!
It is a number from 1 to 99. I'll give you 6 tries.
What's yer guess?50
Too low ,ye scurvy dog!
What's yer guess?75
Too low ,ye scurvy dog!
What's yer guess?87
Too high,landlubber!
What's yer guess?81
Avast! ye got it! Found my secret, ye did!

(猜错了)

>>> ================================ RESTART ================================
>>>
AHOY! I'm the Dread Pirate Roberts,and I have a secret!
It is a number from 1 to 99. I'll give you 6 tries.
What's yer guess?1
Too low ,ye scurvy dog!
What's yer guess?2
Too low ,ye scurvy dog!
What's yer guess?3
Too low ,ye scurvy dog!
What's yer guess?4
Too low ,ye scurvy dog!
What's yer guess?5
Too low ,ye scurvy dog!
What's yer guess?6
Too low ,ye scurvy dog!
No more guesses! Better luck next time, matey!
The secret number was 24

  

令我惊讶的是这一句:

print "The secret number was",secret

  怎么直接就能显示值得呢?感觉异常的怪!

还有一个,语句后边没有分号,if elif else while 后边要加冒号。

My God !!!

print secret,guess,tries

这样子居然都行。OK第二个程序运行完毕!

《与小卡特一起学Python》 Code2的更多相关文章

  1. 《与小卡特一起学Python》Code1

    print "I love pizza!" print "pizza " * 20 print "yum " * 40 print &quo ...

  2. 《与小卡特一起学Python》 Code6 注释

    """这是一个包括多行的注释, 使用了三重引号字符串. 这不完全是注释,不过也可以相当于注释. """ #***************** ...

  3. 《与小卡特一起学Python》 Code5 for循环

    import time for i in range(10,0,-1): print i time.sleep(1) print "Blast off!" 以上代码循环意思为: 从 ...

  4. 《与小卡特一起学Python》Code4 GUI easygui的使用

    EasyGui是基于TKinter的,所以可以跨平台使用. 使用方法很简单,下载之后解压把easygui.py放到python安装目录下的lib/site-packages/下面即可 import e ...

  5. 《与小卡特一起学Python》Code3 抓取网页中的某个数据

    import urllib2 file = urllib2.urlopen('http://common.cnblogs.com/script/jquery.js') message = file.r ...

  6. 小甲鱼零基础学python第25讲课后习题动手练习--通讯录

    小甲鱼零基础学python第25讲课后习题动手练习---通讯录 **************************通讯录要求******************************* 输入指令: ...

  7. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  8. 小测几种python web server的性能

    http://blog.csdn.net/raptor/article/details/8038476 因为换了nginx就不再使用mod_wsgi来跑web.py应用了,现在用的是gevent-ws ...

  9. 简学Python第二章__巧学数据结构文件操作

    #cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...

随机推荐

  1. IOS 调用系统照相机和相册

    /** *  调用照相机 */ - (void)openCamera { UIImagePickerController *picker = [[UIImagePickerController all ...

  2. leetcode Super Pow

    题目描述: superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337. 思路 ...

  3. hp-pa安装oracle和bash

    一.安装oracle数据库 安装之前先进行环境检查: 1.检查系统版本 #uname -a 2.查看内存大小 #/usr/contrib/bin/machinfo | grep -i Memory 3 ...

  4. Leetcode: Encode String with Shortest Length && G面经

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  5. Leetcode: Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  6. C/C++ 结构体 指针 函数传递

    #include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; void s ...

  7. AIX 5L 系统管理技术 —— 存储管理——物理卷

    一.向系统中添加一块硬盘 方法一 该方法适用于在配置之前能够重新启动系统的情况.在系统启动时,就会运行cfgmgr命令,它可自动配置系统中的新设备.当完成了系统启动后,以root用户进入系统,用lsp ...

  8. C# BackgroundWorker组件学习入门介绍

    C# BackgroundWorker组件学习入门介绍 一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用BackgroundWorker来完成这一功能 ...

  9. asp.net XMLHttpRequest 进度条以及lengthComputable always false的解决办法

    一直用ajax好长时间了,对其原理也有一些了解,最近由于项目需要,使用ajax异步进度条的效果,就研究了一下,用原生的XMLHttpRequest实现进度条函数,XMLHttpRequest有以下函数 ...

  10. 提供一个表单,进行post数据处理

    var strContent = "aaaaa"; html.Append("<html><body><form id='postUploa ...