Python中控制语句

来源:互联网 发布:遗传算法tspmatlab程序 编辑:程序博客网 时间:2024/06/11 17:35

浅析概念

while循环

import randomsecret=int(random.uniform(0,10))print("我想的数字在0到10之间,猜猜吧!")guess=11while guess!=secret:    guess=int(input("输入猜的数字吧!"))print("Well Done!")

结果:

我想的数字在010之间,猜猜吧!输入猜的数字吧!3输入猜的数字吧!2输入猜的数字吧!1输入猜的数字吧!4输入猜的数字吧!5输入猜的数字吧!6输入猜的数字吧!7输入猜的数字吧!8输入猜的数字吧!9输入猜的数字吧!10输入猜的数字吧!0Well Done!>>> 

for循环

下列例子都是合法的:

>>> for i in [1,2,3,4,5,6,7,8,9,10,11,12]:    print(i,"times 12 is",i*12)1 times 12 is 122 times 12 is 243 times 12 is 364 times 12 is 485 times 12 is 606 times 12 is 727 times 12 is 848 times 12 is 969 times 12 is 10810 times 12 is 12011 times 12 is 13212 times 12 is 144
>>> for i in (1,2,3,4,5,6):    print (i,"times 12 is",i*12)1 times 12 is 122 times 12 is 243 times 12 is 364 times 12 is 485 times 12 is 606 times 12 is 72
>>> for i in {1,2,3,4,5,6}:    print (i,"times 12 is",i*12)1 times 12 is 122 times 12 is 243 times 12 is 364 times 12 is 485 times 12 is 606 times 12 is 72
for i in "123456789":    print (i,"times 12 is",i*12)1 times 12 is 1111111111112 times 12 is 2222222222223 times 12 is 3333333333334 times 12 is 4444444444445 times 12 is 5555555555556 times 12 is 6666666666667 times 12 is 7777777777778 times 12 is 8888888888889 times 12 is 999999999999#上面这种方法虽然不报错,但没有得到我们想要的结果,它展示了使用错误数据时会出现什么结果。
# 注意区分:# { } 在python中代表集合# [ ] 在python中代表列表# ( ) 在python中代表元组

特别的是,字典这种数据类型的处理与其他类型数据不太一样

>>> real_name={"Benny":"Benjamin Everard","Alex":"Alexander Bradbury"}>>> for key,value in real_name.items():    print("The real name of "+ key+"is"+value)The real name of BennyisBenjamin EverardThe real name of AlexisAlexander Bradbury#for循环的查询方式类似于SQL语句

值得注意的是:当使用集合时,就不能控制访问数据的顺序;一般情况下,如果使用for循环,需要使用列表或元组,而不是集合或者字典。


深入理解

寻找素数

包含循环的嵌套调用

>>> for i in range(1,100,2):    is_prime=True    for k in range(2,i):        if(i%k==0):            print(i,"is divisible by",k)            is_prime=False            break    if is_prime:        print(i,"is prime")1 is prime3 is prime5 is prime7 is prime9 is divisible by 311 is prime13 is prime15 is divisible by 317 is prime19 is prime21 is divisible by 323 is prime25 is divisible by 527 is divisible by 329 is prime31 is prime33 is divisible by 335 is divisible by 537 is prime39 is divisible by 341 is prime43 is prime45 is divisible by 347 is prime49 is divisible by 751 is divisible by 353 is prime55 is divisible by 557 is divisible by 359 is prime61 is prime63 is divisible by 365 is divisible by 567 is prime69 is divisible by 371 is prime73 is prime75 is divisible by 377 is divisible by 779 is prime81 is divisible by 383 is prime85 is divisible by 587 is divisible by 389 is prime91 is divisible by 793 is divisible by 395 is divisible by 597 is prime99 is divisible by 3

if语句

#一个if语句最多执行一段代码(多个elif语句并列情况),例如以下例题,程序只判断10和2的关系,不会再判断10与5的关系。num=int(input("enter a number:"))if num%2==0:    print("Your number is divisible by 2")elif num%3==0:    print("Your number is divisible by 3")elif num%5==0:    print("Your number is divisible by 5")else:    print("Your number is not divisible by 2,3 or 5")

结果:

enter a number:10Your number is divisible by 2

捕获异常

原理:python在执行程序出错后会返回错误类型,借此来实现排错。

is_number=Falsenum=0while not is_number:    is_number=True    try:        num=int(input("enter a number"))    except ValueError:        print("i said a number!")        is_number=Falseif num%2==0:    print("Your number is divisible by 2")elif num%3==0:    print("Your number is divisible by 3")elif num%5==0:    print("Your number is divisible by 5")else:    print("Your number is not divisible by 2,3 or 5")

结果:

enter a number dashi said a number!enter a number3Your number is divisible by 3

练习:修改开始时的猜数字游戏

import randomsecret=int(random.uniform(0,10))print("我想的数字在0到10之间,猜猜吧!")guess=11while guess!=secret:    try:        guess=int(input("Take a guess!"))    except ValueError:        print("i said a number!")print("Well Done!")

结果:

我想的数字在010之间,猜猜吧!Take a guess!di said a number!Take a guess!1Take a guess!2Take a guess!3Well Done!
原创粉丝点击