【发布时间】: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 应该等于上一次迭代的输出 x1 和 x3 应该等于 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