from random import randint,choice
def everysetp(n):
half = n / 2
num = 1
lst = []
while True:
i = 2 ** num - 1
if i >= n:
break
if i >= half:
lst.append(n - i)
num += 1
if lst:
return choice(lst)
else:
return randint(1,int(half))
def game(n):
while n > 1:
print("轮到你的回合,还剩%d的物品" % n)
while True:
try:
num_y = int(input("你要拿走的数量"))
assert 1 <= num_y <= n // 2
break
except:
print("请输入1到%d之间数量的物品" % n//2)
n -= num_y
if n == 1:
print("你赢了")
n -= everysetp(n)
else:
return print("你输了")
game(100)