我的程序非常复杂,我在下面提供了一个更简单的版本:
while True:#This first part asks the user if he wants to run in deterministic mode(explained later) If he does then deterministic is given a value of 1, else, it is given a value of two.
mode= input("Would you like to start the program in deterministic mode?(y/n)")
for i in mode:
if i not in ('yYnN'):
print('Please enter either a \'y\' or an \'n\', Please try again')
break
else:
break
if mode=='y' or mode== 'Y':
deterministic= 1
elif mode=='n' or mode=='N':
deterministic= 2
现在下面是一些使用上述代码的代码。这是一个 if-elif 语句,如果确定性为 ==1,则将运行一个函数,如果确定性==2,则将运行另一个函数。
if deterministic==2:#position,time, and energy variables will come from the function updateDisplayAllVar(....)
position,time,energy=updateDisplayAllVar(selection,position,time,energy)
elif deterministic==1:#position,time, and energy variables will come from the function deterministic(....)
position,time,energy=deterministic(selection,position,time,energy)
现在我将向您展示上述代码中的两个函数。我相信问题出在他们身上。第一个函数给出一个随机数,并且每次都给出一个可能不同的随机数。第二个函数每次使用 .seed 将随机数限制为相同的“随机”数。
def updateDisplayAllVar(选择,位置,时间,能量):
if selection=='w' or selection =='W':
energy= energy
position= position+1
time=time+1
elif selection=='j' or selection =='J':
energy= energy-1
position= position+2
time=time+1
elif selection=='r' or selection =='R':
energy= energy-random.randrange(1,2+1)
position= position+random.randrange(1,4+1)
time=time+1
elif selection=='f' or selection =='F':
energy= energy- random.randrange(3,5+1)
position= position+random.randrange(3,8+1)
time=time+1
def deterministicMode(selection,position,time,energy):
if selection=='w' or selection =='W':
energy= energy
position= position+1
time=time+1
elif selection=='j' or selection =='J':
energy= energy-1
position= position+2
time=time+1
elif selection=='r' or selection =='R':
random.seed(1)
energy= energy-random.randrange(1,2+1)
position= position+random.randrange(1,4+1)
time=time+1
elif selection=='f' or selection =='F':
random.seed(3)
energy= energy- random.randrange(3,5+1)
position= position+random.randrange(3,8+1)
time=time+1
Now my problem is... that, with this code, even if I do not choose to be in deterministic(no random number) mode, I still get numbers that are not random and are based on the second function. That is, even though there are two functions up there and I'm asking each function to run based on the users input... It is always deterministicMode(selection,position,time,energy)
that runs. Does anyone know a fix. Please let me know if anything is unclear and I will fix it.