total = 20
while total+1 >0:
m = int(input('请输入你要拿的根数:'))
total -= m
if total <=0:
print('游戏结束')
break
if total < 5:
n = total
total -= n
else:
n = total % 5
total -= n
print('你拿了%d,机器拿了%d,剩下%d' % (m , n, total+1))
#最简单的解法
total = int(input('请输入火柴根数:'))
n = int(input('请输入最大可拿火柴根数:'))
while total > 0:
m = int(input('请输入你要拿的根数:'))
total -= m
j = n + 1 - m
total -= j
print('你拿了%d,机器拿了%d,剩下%d' % (m , j, total))
#另一种解法
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
if total >0:
print('计算机拿走了%d根火柴' % (5 - num))
total -= 5- num
print('你拿到了最后一根火柴,你输了!')
if __name__ == '__main__':
main()
#电脑随机拿火柴
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
if total >0:
com = randint(1, min(4, total))
print('计算机拿走了%d根火柴' % com)
total -= com
if total == 0:
print('计算机拿到最后一根火柴,你赢了!')
else:
print('你拿到了最后一根火柴,你输了!')
if __name__ == '__main__':
main()
total = 20while total+1 &gt;0: m = int(input('请输入你要拿的根数:')) total -= m if total &lt;=0: print('游戏结束') break if total &lt; 5: n = total total -= n e...