逻辑还是要多练练的!
""" 小游戏:看谁先数到100 游戏规则:两个人轮流从1开始数数,每人每次数且只能数1到3个数,先数到100即为赢家 """ print(" 看谁先数到100 ".center(60,"*")) def func(n, x, flag): """ n:目标数 x:步进值 flag:计算机是否先出。先出为0,后出为1 """ count = 0 y = x + 1 # 关键值 if flag == 1: # 人先出 people_num = int(input()) if not (people_num > 0 and people_num < y): print("你的输入有错误,你耍赖,不和你玩了。") exit() count = count + people_num print("人数 {} ,现在是 {}".format(people_num,count)) n = n - people_num while True: if n % y == 0: computer_num = 1 count = count + computer_num print("计算机数 {} ,现在是 {}".format(computer_num,count)) people_num = int(input()) if not (people_num > 0 and people_num < y): print("你的输入有错误,你耍赖,不和你玩了。") break count = count + people_num print("人数 {} ,现在是 {}".format(people_num,count)) n = n - computer_num - people_num if n < y: print("计算机认输啦!你赢啦!!") break elif n % y != 0: computer_num = n % y count = count + computer_num print("计算机数 {} ,现在是 {}".format(computer_num,count)) people_num = int(input()) if not (people_num > 0 and people_num < y): print("你的输入有错误,你耍赖,不和你玩了。") break count = count + people_num print("人数 {} ,现在是 {}".format(people_num,count)) n = n - computer_num - people_num if n < y: print("很遗憾,你输了!!") break func(100,3,1)
原文:https://www.cnblogs.com/ooops/p/14584736.html