我在试着做蒙蒂霍尔的模拟。目标是尝试并能够运行多个模拟,以便我有一个足够大的样本大小。
x = 0
i = 0
if modeChoice == "Automatic":
swapOrNot = input("Do you want to test Keeping or Swapping")
tests = int(input("How many tests do you want to run?")
if swapOrNot == "Keeping":
while i < tests:
i + 1
cars = random.randint(1, 3)
randchoice = random.randint(1, 3)
if randchoice == cars:
x + 1这不是所有的代码,但是到目前为止唯一不能工作的部分。Python告诉我有一个问题
if swapOrNot == "Keeping":
while i < tests:具体来说,它与第一行if语句有问题。我不明白为什么这句话行不通。while循环的主要目标是能够多次运行。
如果我不能提供足够的信息,我很抱歉,我很乐意提供更多:)
发布于 2021-12-10 00:04:14
这是一个工作版本,基于上面的评论。作为一种修饰性的更改,在input()提示符中添加了一个空格,以使用户的输入更加优雅:
modeChoice = 'Automatic'
x = 0
i = 0
if modeChoice == "Automatic":
swapOrNot = input("Do you want to test Keeping or Swapping? ")
tests = int(input("How many tests do you want to run? "))
print(tests)
if swapOrNot == "Keeping":
while i < tests:
i += 1
cars = random.randint(1, 3)
randchoice = random.randint(1, 3)
if randchoice == cars:
x += 1
print(f'ran {tests} tests, got cars {x} times')(显然,您必须实现“交换”,以便为Monty问题创建一个精确的模型。)
https://stackoverflow.com/questions/70298031
复制相似问题