九九乘法表:
完整版打印
for i in range(1,10):
for j in range(1,10):
print('%s*%s=%s'%(i,j,i*j))
print('\n')
'''
i = 1
while i<10:
j = 1
while j<10:
print('%s*%s=%s\t'%(j,i,i*j),end='')
j+=1
i+=1
print('\n')
'''
正三角打印
i = 1
while i<10:
j = 1
while j<=i:
print('%s*%s=%s\t'%(j,i,i*j),end='')
j+=1
i+=1
print('\n')
if elif例子:
salary = int(input('输入工资:'))
if salary<3000:
print('屌丝......')
elif salary<5000:
print('土鳖......')
elif salary<8000:
print('温饱......')
elif salary<12000:
print('普通......')
elif salary<20000:
print('小康......')
else:
print('土豪......')
判断变量名是否正确:
def f(s):
if s[0].isdigit():
return('不符合')
else:
for i in s:
if (i.isalpha())or(i.isdigit())or(i=='_'):
continue
else:
return('不符合')
return('符合')
print(f(input('请输入字符串:')))
猜拳游戏:
import random
while True:
computer = random.randint(0,2)
player = int(input('请输入:剪刀(0) 石头(1) 布(2):'))
if player!=0 and player!=1 and player!=2:
print('输入的数字不符合要求,请重新选择')
else:
if (computer==0 and player==1)or(computer==1 and player==2)or(computer==2 and player==0):
print('恭喜你,赢啦......')
elif computer==player:
print('平手......')
else:
print('输啦......')
answer = input('****还玩吗(Y/N)?****')
if answer!='Y':
break
print('计算机选择的是%s,用户选择的是%s.'%(computer,player))
print('game over......')