99乘法表
# while
i = 1
while i < 10:
j = 1
while j <= i:
print(f'{j}*{i}={i * j}', end=' ')
j += 1
i += 1
print('\n')
# for
for i in range(10):
for j in range(1, i+1):
print(f'{j}*{i}={i*j}', end=' ')
print('\n')
青蛙跳台阶
# lambda
fib = lambda n: n if n <= 2 else fib(n - 1) + fib(n - 2)
print(fib(6))
# def
def fib(n):
if n == 1 or n==2:
return n
else:
return fib(n - 1) + fib(n - 2)
print(fib(6))
现有5升和3升的杯子,取四升的水
一
1、将5杯子装满然后倒入3升杯子中,再将3升杯子倒空
2、把5升杯子剩余的2杯水倒入3升杯子并将5升的杯子装满
3、此时3升杯子中的剩余2升,然后将5升中的一升倒入3升中,得到4升的水
二
1、将3升杯子装满,然后倒入5升的杯子中,此时5升未装满,可在装2升
2、再将3升杯子装满,倒入5升的杯子中,3升的杯子剩余1升
3、将5升杯子倒掉,将3升中的水倒入5升中,此时5升杯子中有1升
4、再将3升杯子装满,再到入5升杯子中,得到4升的水
倒直角三角形
for i in range(6,0,-1):
for j in range(i,6):
print(' ',end=' ')
for j in range(i,0,-1):
print('*',end=' ')
print('\n')
倒等腰三角形
for i in range(6,0,-1):
for j in range(i,6):
print(' ',end=' ')
for i in range(i*2-1,0,-1):
print('*',end=' ')
print('\n')
猜数字
import random
item = random.randint(1,100)
for i in range(10, 0, -1):
if i == 0:
break
number = input('亲,请输入数字:')
if int(number) == item:
print('猜对了')
break
elif item > int(number):
print('小了,还有{}次机会'.format(i - 1))
elif item < int(number):
print('大了,还有{}次机会'.format(i - 1))
猜拳
import random
while True:
computer = random.randint(0, 2)
try:
play01 = input('剪刀(0) 石头(1) 布(2):')
if play01 == 'end':
break
play01 = int(play01)
if computer == play01:
print('平局')
else:
if computer == 0 and play01 == 1:
print('您银了')
elif computer == 0 and play01 == 2:
print('电脑银了')
elif computer == 1 and play01 == 0:
print('电脑银了')
elif computer == 1 and play01 == 2:
print('您银了')
else:
print('输入不合规')
except:
print('输入不合法')