我正在尝试为约瑟夫斯问题编写一些代码,但我不断收到一个奇怪的错误。我的代码如下所示:
import math
power = 2
originalnumber = int(input("how many to start?"))
powerof2 = math.log(originalnumber,power)
if type(powerof2) == int:
powerof2tf = True
winningseat = 1
else:
powerof2tf = False
newnumber = originalnumber
while True:
newnumber = newnumber - 1
variabletest = type(math.log(newnumber,power))
if variabletest == int:
break
else:
pass
winningseat = 1+2*(originalnumber-newnumber)
print("the winning seat is", winningseat)
当我尝试运行它时,它给了我这个错误:
ValueError Traceback(最近一次调用最后一次)
在 ()
12 import math
13 newnumber = newnumber - 1
---> 14 variabletest = type(math.log(newnumber,power))
15 if variabletest == int:
16 break
ValueError:数学域错误
有任何想法吗?我不知道如何解决这个问题。