标签:dash 字符串 string computer 字符 说明 layer pass not
函数的流程控制:
if...else... a = 100 b = 200 if a == 100 and b ==300: print(‘100,200‘) elif b == 200: print(‘等于200‘) else: print(‘不等于100也不等于200‘)
while 条件
循环体:
如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
如果条件为假,那么循环体不执行,循环终止
while a = 0 while a <= 10: if a%2 == 0: print(a) a += 1 l1 = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘] a = 0 while a < len(l1): print(l1[a]) a += 1
continue,break,pass
break:跳出循环,并且break往后的代码将不会被执行
continue:跳出本次循环,继续下一次循环,并且continue往后的代码将不会被执行
pass;占位符,不执行任何操作,只是占位
while True: result = input(‘==>:‘) if not result: continue else: print(result) n = 0 while n <= 15: if n == 10: pass else: print(n) n += 1
流程控制之for循环:
1 迭代式循环:for,语法如下
for i in range(10):
缩进的代码块
2 break与continue(同上)
for:遍历出每个元素,for可以循环字符串,列表,字典,集合,元组
for循环字典循环出来的是key
a = [1,2,3]
b = ‘abcdefg‘
d = {‘name‘:‘小米‘,‘age‘:25}
for i,u in d.items():
print(i)
for i in range(11,0,-2):
print(i)
实例:
猜拳游戏,石头剪刀布。
import random print(‘*‘*20) print("***欢迎来到猜拳游戏***\n" "提示(请输入数字)\n" "1(石头),2(剪刀),3(布),0(退出游戏)\n" "赢一局得3分,输一局扣3分,平局不得分\n") y = 0 p = 0 s = 0 f = 0 while True: play = input("请出拳:") computer = random.randint(1, 3) res = play.isdigit() if res == True: player = int(play) if player <= 3 and player >=0: if (computer==1 and player==2) or (computer==2 and player==3) or (computer==3 and player==1): print("你输了,请再接再厉") s += 1 elif computer == player: print("平局") p += 1 elif player == 0: break else: print("你赢了!请继续加油") y += 1 else: print("输入错误,请重新输入!" "提示:" "1(石头),2(剪刀),3(布),0(退出游戏)") else: print("输入错误,请输入数字") f = y*3 - s*3 print("你赢了%d局,输了%d局,平局%d,共计得分%s"%(y,s,p,f))
定义一个函数:
你可以定义一个由自己想要功能的函数,以下是简单的规则:
eg: def ChangeInt(a): a = 10 b = 2 ChangeInt(b) print(b) >>> 2 "修改传入的列表" def changeme(mylist): mylist.append([1, 2, 3, 4]) print("函数内取值: ", mylist) return mylist = [10, 20, 30] changeme(mylist) print("函数外取值: ", mylist) >>> 函数内取值: [10, 20, 30, [1, 2, 3, 4]] >>> 函数外取值: [10, 20, 30, [1, 2, 3, 4]] "打印任何传入的字符串" def printme(str): print(str) return printme(str="My string") >>> My string # 可写函数说明 "打印任何传入的字符串" def printinfo(name, age): print("Name: ", name) print("Age ", age) return printinfo(age=50, name="miki"); >>> Name: miki >>> Age 50 "打印任何传入的参数" def printinfo(args,*vartuple): print(args) for var in vartuple: print(var) return printinfo(10) printinfo(70, 60, 50) >>> 10 >>> 70 >>> 60 >>> 50
标签:dash 字符串 string computer 字符 说明 layer pass not
原文地址:https://www.cnblogs.com/twoo/p/11750304.html