【问题标题】:Python while loop, if then conditions not working properlyPython while循环,如果条件不正常工作
【发布时间】:2018-09-16 11:09:44
【问题描述】:

我是 Python 新手,正在尝试我的第一个 while 循环。

以下代码旨在迭代用户在 number_of_moves 变量中定义的公式。

i = 1 时,它应该执行一个公式,然而当i > 1 它应该执行另一个公式。所以我在公式中定义了一个 if else 语句

问题是当i > 1 没有选择第二个公式,而是继续使用第一个定义的公式,即(22695477 * x + 1) % 2 ** 31

实际上,else 语句 x2 应该等于上一次迭代的输出 x1x3 应该等于 x2 的输出值...等等on ... 使用这个公式(22695477 * x2 + 1) % 2 ** 31

print("Choose the type of game(1:Easy;2 Difficult)")
levelinput = int(input())

print("")


print("Enter the number of moves")

number_of_moves = int(input())
i =1
x = 79
randomvalue = (22695477*x+1)%2**31
x2 = randomvalue
machine = int()


while i <= number_of_moves:
    print("")
    print("Choose your move number", i ,"(0 or 1)")

    move_selection = int(input())

    if i  == 1:
        randomvalue = (22695477*x+1)%2**31

    else:
        randomvalue = (22695477*x2+1)%2**31

    i = i +1


    if randomvalue <= 2**31:
        machine == int(0)
    else:
        machine == int(1)

    def resultgame (move_selection,machine):
        if move_selection == machine:
            return("Computer Wins")
        else:
            return("Player Wins")

    result = resultgame

    print("player = ", move_selection, "machine = ", machine,"-", result(move_selection,machine))

【问题讨论】:

  • 你永远不会改变i,所以while循环将永远循环,第二个函数总是选择0选项。 if i == 1: 应该是 if move_selection == 1 吗?
  • 没有移动选择是二进制类别,1 或 0 - 与问题无关,因此在此处删除..thx
  • 小心缩进。您显示的代码不会运行。
  • line 19: IndentationError: unindent does not match any outer indentation level
  • @Probit 您在此处发布的代码运行不正常。请显示minimal reproducible example,以便我们知道实际问题是什么。

标签: python loops while-loop


【解决方案1】:

好吧,你用最好的方式编写的代码并不好。

def LinearCong (x,x2):
  if i == 1:
    randomvalue = (22695477*x+1)%2**31
  else:
    randomvalue = (22695477*x2+1)%2**31

  return randomvalue

 x2 = randomvalue
 i+=1

不应将此函数放入 while 循环或任何与此相关的循环中。现在我建议忘记使用函数,即def function(): 虽然您所写的语法有些正确,但丝毫没有正确实现。

这是我对您尝试以更简单的形式执行的操作的解释。

i =1
number_of_moves = 10
x = 10000

#problem understanding
#included a randomvalue variable here becaause you want it to be assigned to var x2
#unless randomvalue is declared here the else statement would never work
randomvalue = (22695477*x+1)%2**31
x2 = randomvalue

while i <= number_of_moves:
    print("")
    print("Choose your move number", i ,"(0 or 1)")
    move_selection = int(input())

    if move_selection == 1:
        randomvalue = (22695477*x+1)%2**31
        break
    else:
        #here you are asking for var x2 which would have no value unless declared above
        #randomvalue as a variable is created here so x2 would have nothing to refer to
        randomvalue = (22695477*x2+1)%2**31
        #in this case, we are reassigning our already declared variable
        break

print(randomvalue)

我编写的这段代码存在很大问题,但我不完全理解您要实现的目标是什么。也许有关游戏的更多信息会有所帮助。

【讨论】:

  • 如果这个问题这么不清楚,为什么还要回答这个问题?
  • @colidyre 因为这可能会帮助他弄清楚自己在做什么
  • @Nick Eu,感谢 Nick 的帮助,但是 x2 仍然没有像 else 语句中那样被拾取。考虑到您的一些建议,我已经修改了上面的代码。然而也无济于事..
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 2021-12-25
  • 2015-05-14
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多