Python—入门例程(持续更新)
作者:互联网
目录
01-猜拳游戏
if语句多分支,多条件的演练
#person-人 computer-计算机
#导入模块
import random
computer=random.randint(0,2)#计算机产生随机数
person=int(input("请出拳:[0:石头 1:剪刀 2:布]:"))#人
print("机器人出:",computer)
if person==0 and computer==1:
print("厉害了,你赢了")
pass
elif person==1 and computer==2:
print("厉害了,你赢了")
pass
elif person==2 and computer==0:
print("厉害了,你赢了")
pass
elif person==computer:
print('不错,平手了')
pass
else:
print("你输了!")
pass
02-九九乘法表
while循环嵌套
row=1
while row<=9:
col=1
while col<=row:
print("%d*%d=%d"%(col,row,row*col),end=" ")#每列打印完末尾+空格
col+=1
pass
print()#打印完一行+换行
row+=1
pass
改变方向:
row=9
while row>=1:
col=1
while col<=row:
print("%d*%d=%d"%(col,row,row*col),end=" ")#每列打印完末尾+空格
col+=1
pass
print()#打印完一行+换行
row-=1
pass
03-打印三角形
while循环嵌套
打印直角三角形:
row=1
while row<=7:
col=1
while col<=row:
print('*',end=' ')
col+=1
pass
print()
row+=1
打印等腰三角形:
row=1
while row<=5:
j=1
while j<=5-row:#控制打印空格
print(' ',end='')#不换行
j+=1
pass
k=1
while k<=2*row-1:#控制打印*
print('*',end='')
k+=1
pass
print()
row+=1
标签:入门,person,例程,Python,while,computer,pass,print,row 来源: https://blog.csdn.net/m0_65592409/article/details/122777511