- 三元运算符
最大数
a = float(input('请输入第一个数:'))
b = float(input('请输入第二个数:'))
c = float(input('请输入第三个数:'))
my_max = a>b and a or b
my_max = c>my_max and c or my_max
'''
my_max = a
if my_max < b:
my_max = b
elif my_max < c:
my_max = c
'''
print('最大数为:',my_max)
- 元组
从小到大输出
a = int(input('请输入第一个数:'))
b = int(input('请输入第二个数:'))
c = int(input('请输入第三个数:'))
if a>b:
a,b = b,a
if b>c:
b,c = c,b
if a>b:
a,b = b,a
'''
(a,b) = a>b and (b,a) or (a,b)
(b,c) = b>c and (c,b) or (b,c)
(a,b) = a>b and (b,a) or (a,b)
'''
print(a,b,c)
2到100之间的素数
for i in range(2,101):
is_prime = True
#判断是不是素数
for j in range(2,i):
if i%j==0:
is_prime = False
break
if is_prime:
print('%d是素数'%(i))
九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'%(j,i,i*j),end='\t')
print()
图形
#ASCll码
for i in range(5):
for j in range(i+1):
print(chr(65+j),end='')
print()
for i in range(5):
for j in range(4-i):
print(' ',end='')
print('*'*(i+1))
for i in range(5):
for j in range(4-i):
print(' ',end='')
print('*'*2*i,end='')
print('*')
A,B,C,D,E五个人捕鱼后已凌晨,大家便睡觉。早上A第一个醒来,将鱼均分成五份,把多余的一条鱼扔掉,拿走自己的一份,B第二个醒来,也将鱼均分为五份,把多余的一条鱼扔掉,拿走自己的一份。CDE依次醒来,也按同样的方法拿鱼,问他们合伙至少捕了几条鱼。
fash = 1
while True:
total = fash
is_enough = True
for i in range(5):
if (total-1)%5==0:
total = (total-1)//5*4
if i==4:
print(fash)
is_enough = False
fash+=1
if is_enough == False:
break
水仙花数 1m3+5m3+3m3=153
for i in range(2,1000):
if (i//100)**3+(i%100//10)**3+(i%10)**3 == i:
print(i)
找出10000以内的完美数
完美数:除本身外所有因子和等于本身
from time import time
from math import *
star = time()
for i in range(1,10000):
num = 1
for j in range(2,int(sqrt(i))+1):
if i%j==0:
num += j
if j!=i//j:
num += i//j
if num==i:
print(i)
end = time()
print(end-star)
craps赌博游戏
from random import *
player_money = 1000
computer_money = 1000
while True:
stake = int(input('请下注:'))
if stake <= computer_money and stake <= player_money:
is_result = True
r1 = randint(1, 6)
r2 = randint(1, 6)
r3 = r1 + r2
print(r1, r2, r3)
if r1+r2==7 or r1+r2==11:
print('\033[0;35m玩家获胜\033[0m')
player_money += stake
computer_money -= stake
elif r1+r2==2 or r1+r2==3 or r1+r2==12:
print('\033[0;35m电脑获胜\033[0m')
player_money -= stake
computer_money += stake
else:
while is_result:
r1 = randint(1, 6)
r2 = randint(1, 6)
print(r1, r2, r1 + r2)
if r1+r2 == r3:
print('\033[0;31m玩家获胜\033[0m')
player_money += stake
computer_money -= stake
is_result = False
elif r1+r2 == 7:
print('\033[0;31m电脑获胜\033[0m')
player_money -= stake
computer_money += stake
is_result = False
print('玩家剩余余额为:¥%d'%(player_money))
print('电脑剩余余额为:¥%d' % (computer_money))
else:
print('押注金额不能大于余额!')
if player_money <= 0:
print('\033[1;35m电脑获得游戏胜利!!!\033[0m')
break
elif computer_money <= 0:
print('\033[1;35m玩家获得游戏胜利!!!\033[0m')
break