#比较大小 #方法1 from math import max a = (‘请输入第一个整数:‘) b = (‘请输入第二个整数:‘) c = (‘请输入第三个整数:‘) d = max(a, b, c) print(‘其中最大的是%f:‘ %d)
#如何交换两个变量值 a, b = b, a ‘‘‘ temp = a a = b b = temp ‘‘‘ a = int(input(‘a =‘)) b = int(input(‘b =‘)) c = int(input(‘c =‘)) ‘‘‘ if a > b: a, b = b, a if b > c: b, c = c, b if a > b: a, b = b, a print(a, b , c)‘‘‘ (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)
#判断一个数是否是质数 a = int(input(‘请输入一个数:‘)) is_prime = True for b in range(2, a): c = a % b if c == 0: is_prime = False break if is_prime: print(‘%d他是质数!‘ % a) else: print(‘%d他不是质数‘ % a)
#九九乘法表 for row in range(1, 10): for col in range(1, row + 1): print(‘%d*%d=%d‘ % (row, col, row * col), end=‘\t‘)#制表符 print()#换行用,
#百元百鸡,公鸡5元一只,母鸡3元一只,小鸡一元3只,问有一百元,买一百只鸡,有几种买法 for x in range(21): for y in range(34): #for z in range(0, 100, 3): z = 100 - x - y if 5 * x + 3 * y + z // 3 == 100 and z % 3 == 0: print(x, y ,z)
print(0.1 + 0.2 + 0.3) print(0.3 + 0.2 + 0.1) #由于浮点数表示法的问题, #在实际开发的过程中请不要做浮点数的==和!=运算. #实在要用,一定要先转换成int再计算,算完再转换回去.
#找出 1000 以内的水仙花数。 from math import pow for num in range(1000): a = num // 100
#经典的五人分鱼问题 fish = 1 while True: totle = fish
#经典的Craps赌博游戏 from random import randint go_on = False num1 = randint(1, 6) num2 = randint(1, 6) total1 = num1 + num2 print(‘玩家摇出了%d‘ % total1) if total1 == 7 or total1 == 11: print(‘玩家胜‘) elif total1 == 2 or total1 == 3 or total1 == 12: print(‘玩家输‘) else: go_on = True while go_on: num1 = randint(1, 6) num2 = randint(1, 6) total2 = num1 + num2 print(‘玩家摇出了%d‘ % total2) if total2 == total1: print(‘玩家胜!‘) go_on = False elif total2 == 7: print(‘玩家输!‘) go_on = False
is_enough = True for _ in range(5): if (total - 1) % 5 == 0: total = (total - 1) // 5 * 4 else: break is_enough = False if is_enough: print(fish) break fish += 1
b = num // 10 % 10 c = num % 10 if num == pow(a, 3) + pow(b, 3) + pow(c, 3): print(‘%f是水仙花数!‘ %num)
#完美数 from math import sqrt for num in range(10000): sum = 0 for i in range(2, int(sqrt(num)) + 1): if num % i == 0: sum +=i if i != num // i: sum += num // i if num == sun + 1: print(‘%d是完美数‘ %num)