# 排列组合c(n,m)
m = int(input(‘m = ‘)) n = int(input(‘n = ‘)) fm = 1 for num in range(1, m + 1): fm *= num fn = 1 for num in range(1, n + 1): fn *= num fmn = 1 for num in range(1, m - n + 1): fmn *= num print(fm // fn // fmn)
def f(x): """ 求函数x的阶乘 :param x: 任意正整数 :return: x的阶乘 """ y = 1 for z in range(1, x + 1): y *= z return y if __name__ == ‘__main__‘: m = int(input(‘m = ‘)) n = int(input(‘n = ‘)) # 当需要计算阶乘的时候,不用再写循环,而是直接调用已经定义好的函数就可以了。 print(f(m) // f(n) // f(m - n))
# Craps 游戏改进版
from random import randint def roll_dice(n=2): total = 0 for _ in range(n): total += randint(1, 6) return total # 改名字,选中要修改的名字,按中shift + F6 from random import randint money = 1000 while money >0: print(‘玩家总资产:%d‘ % money) while True: debt = int(input(‘清下注:‘)) if 0 < debt <= money: break go_on = False print(‘玩家摇出了%d‘ % roll_dice()) if roll_dice() == 7 or roll_dice() == 11: print(‘玩家胜‘) money += debt elif roll_dice() == 2 or roll_dice() == 3 or roll_dice() == 12: print(‘玩家输‘) money -= debt else: go_on = True while go_on: roll_dice() print(‘玩家摇出了%d‘ % roll_dice()) if roll_dice() == roll_dice(): print(‘玩家胜!‘) money += debt go_on = False elif roll_dice() == 7: print(‘玩家输!‘) money -= debt go_on = False print(‘你已经破产!‘)
# 判断质数
from math import sqrt def is_prime(num): for factor in range(2,int(sqrt(num)) + 1): if num % factor == 0: return False return num !=1 and True or False # 通过下面的if条件,可以在导入模块时不去执行下面的代码。 # ctrl + 鼠标左键 可以查找解释定义。 if __name__ == ‘__main__‘: num = int(input(‘请输入一个数判断是不是质数:‘)) print(is_prime(num))
# 最小公倍数和最大公约数
def lcm(x, y): # if x > y: # (x, y) = (y, x) ‘‘‘ 求最小公倍数 :param x: 任意正整数 :param y: 任意正整数 :return: x和y的最小公倍数 ‘‘‘ (x, y) = (y, x) if x > y else (x, y) for i in range(y, x * y + 1): if i % x == 0 and i % y == 0: return i def gcd(x, y): # if x > y: # (x, y) = (y, x) #文档注释 """ 计算最大公约数 :param x: 任意正整数 :param y: 任意正整数 :return: 最大公约数 """ # (x, y) = (y, x) if x > y else (x, y) i = x while i > 0: if x % i ==0 and y % i == 0: # range(x, 0, -1) 从大到小。 return i i -= 1 if __name__ == ‘__main__‘: x = int(input(‘请输入第一个数x=‘)) y = int(input(‘请输入第二个数y=‘)) print(‘最小公倍数:‘, lcm(x,y)) print(‘最大公约数:‘, gcd(x, y))
# 判断回文质数
from math import sqrt def is_prime(num): for factor in range(2,int(sqrt(num)) + 1): if num % factor == 0: return False return num !=1 and True or False def is_palindrome(num): ‘‘‘ 判断是不是回文数 :param num: 非负整数 :return: 是回文数返回True,不是回文数返回Flase. ‘‘‘ temp = num total = 0 while temp > 0: total = total * 10 + temp % 10 temp //= 10 return num == total #and和or运算符都是带短路功能的运算符 #如果and左边的表达式是Flase,那么右边的表达式被短路 #如果or左边的表达式是True,那么右边的表达式被短路 #所以左右两边的表达式放置的顺序可能会对执行效率产生明显的影响。 if __name__ == "__main__": num = int(input(‘请输入一个数:‘)) print(is_palindrome(num)) if is_palindrome(num) and prime_number(num): print(‘%是回文质数‘ % num) else: print(‘%f不是回文质数‘ % num)
# 21根火柴的游戏,谁拿到最后一根,谁就输。每次拿的范围[1,4].保证计算机获胜。
def game_match(): total = 21 while True: my_number = int(input(‘请输入你拿的火柴数:‘)) if 1 <= my_number <= 4: com_number = 5 - my_number total -= 5 print(‘电脑拿完还有多少根‘, total) if total == 1: print(‘你输了‘) break else: print(‘请输入正确的火柴数!‘) if __name__ == ‘__main__‘: game_match()
# 21根火柴游戏公平版
from random import randint def main(): total = 21 while total > 0: print(‘还剩下%d根火柴‘ % total) while True: num = int(input(‘请输入你要拿的火柴数:‘)) if 1 <= num <= 4 and num <= total: break total -= num print(‘你拿完之后还剩%d‘ % total) if total > 0: com = randint(1,min(4,total)) print(‘电脑拿了%d根火柴‘ % com) total -= com if total == 0: print(‘电脑拿走了最后一根火柴,电脑输了!‘) break if total == 0: print(‘你拿走了最后一根火柴,你输了1‘) if __name__ == ‘__main__‘: main()