• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

冯唐--python学习日记(5)

发布: 2007-7-04 20:06 | 作者: admin | 来源:  网友评论 | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网

例程:

>>> import random
>>> import sys
>>> SecretNumber= random.randint(1,100)
>>> print "i'm thinking of a number between 1 to 100"
i'm thinking of a number between 1 to 100
>>> while(1):
 print "Guess my number."
 NumberGuess = int(sys.stdin.readline())
 if (NumberGuess == SecretNumber):
  print "Correct! Choosing a new number..."
  SecretNumber = random.randint(1,100)
 elif (NumberGuess > SecretNumber):
  print "lower."
 else:
  print "Eigher."

Guess my number.
9
Eigher.
Guess my number.
5
Eigher.
Guess my number.
99
lower.
Guess my number.
56
Eigher.
Guess my number.
77
Eigher.
Guess my number.
88
Eigher.
Guess my number.
95
lower.
Guess my number.
90
Correct! Choosing a new number...
Guess my number.
90
Eigher.
Guess my number.
95
lower.
Guess my number.
95
lower.
Guess my number.
93
lower.
Guess my number.
91
Correct! Choosing a new number...
Guess my number.
92
lower.
Guess my number.
80
Eigher.
Guess my number.
86
lower.
Guess my number.
5
Eigher.
Guess my number.
85
Correct! Choosing a new number...
Guess my number.

一、if语句

if (condition):

       expression

else:

       expression

二、while语句

while (expression):

          if (condition):

                     expression

          elif:

                     expression

          ...

          else:

                    expression

 三、for循环

for in :

      (loop body)

 例

>>> import string
>>> def Encode(MessageString):
 EncodeList = []
 for Char in MessageString:
  EncodeList.append("%x" % ord(Char))
 return EncodeList

>>> def Decode(SecretMessage):
 DecodedList = []
 for hexvalue in SecretMessage:
  DecodedList.append(chr(int(hexvalue,16)))
 return string.join(DecodedList,"")

>>> if (__name__=="__main__"):
 SecretMessage = Encode("remember to drink your Ovaltine!")
 print SecretMessage
 print Decode(SecretMessage)

['72', '65', '6d', '65', '6d', '62', '65', '72', '20', '74', '6f', '20', '64', '72', '69', '6e', '6b', '20', '79', '6f', '75', '72', '20', '4f', '76', '61', '6c', '74', '69', '6e', '65', '21']
remember to drink your Ovaltine!
>>>

中断和继续

continue和break

例打印0~99之间的偶数,循环一次,删除没有处理的序列的第一个数据,打印第二个元素。

>>> MyRange = range(100)
>>> for x in MyRange:
 print x
 del MyRange[0]

错误处理机制

try块和finally块

try块捕捉错误

finally是放置必须执行的代码块

四、利用断言进行调试

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网