九九乘法表:
完整版打印
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('輸入的數(shù)字不符合要求,請重新選擇')
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......')