再次开始游戏

  • 本文关键字:游戏 开始 python
  • 更新时间 :
  • 英文 :


我希望我的程序在用户在第 55 行输入字母"y"时再次启动游戏,但由于某种原因我无法重新启动。现在它是"print(switch(",我也尝试过"print(str(switch((",什么都没有。我希望如果用户输入"y",游戏会重新开始。我做错了什么?

import random
win = 0
lose = 0
switch = int(input("Stay or switch? (0-stay/1-switch):")) == 1

n = 0
while n < 100000:
door_values = [0, 0, 0]
removable_doors = [0, 1, 2]
available_doors = [0, 1, 2]
car_place = random.randint(0, 2)
door_values[car_place] = 1
removable_doors.remove(car_place)
door_chosen = random.randint(0, 2)
available_doors.remove(door_chosen)
if door_chosen != car_place:
removable_doors.remove(door_chosen)
door_removed_by_host = removable_doors[random.randint(0, len(removable_doors)-1)]
available_doors.remove(door_removed_by_host)
if switch:
assert(len(available_doors) == 1)
door_chosen = available_doors[0]
if car_place == door_chosen:
win += 1
else:
lose += 1
n+=1
print('win=%s'%str(win))
print('lose=%s'%str(lose))
print("This is the number of wins if the user switched", 
round((win)/1000), "%")
print("This is the number of wins if the user didn't switch", 
round((lose)/1000),"%")
answer = input("Play again? (y/n): ")
if answer == 'y':
print(switch)
elif answer == 'n':
print("Game Over")

您只检查用户是否输入 y 或 n,而不执行任何操作。把它放在一个while True循环中,就像这样:

import random
while True:
win = 0
lose = 0
switch = int(input("Stay or switch? (0-stay/1-switch):")) == 1

n = 0
while n < 100000:
door_values = [0, 0, 0]
removable_doors = [0, 1, 2]
available_doors = [0, 1, 2]
car_place = random.randint(0, 2)
door_values[car_place] = 1
removable_doors.remove(car_place)
door_chosen = random.randint(0, 2)
available_doors.remove(door_chosen)
if door_chosen != car_place:
removable_doors.remove(door_chosen)
door_removed_by_host =removable_doors[random.randint(0,len(removable_doors)-1)]
available_doors.remove(door_removed_by_host)
if switch:
assert(len(available_doors) == 1)
door_chosen = available_doors[0]
if car_place == door_chosen:
win += 1
else:
lose += 1
n+=1
print('win=%s'%str(win))
print('lose=%s'%str(lose))
print("This is the number of wins if the user switched", 
round((win)/1000), "%")
print("This is the number of wins if the user didn't switch", 
round((lose)/1000),"%")
answer = input("Play again? (y/n): ")
if answer == 'y':
print(switch)
elif answer == 'n':
print("Game Over")
break

如果用户输入n,这将中断循环,如果输入y,这将继续。

print函数只会在屏幕上打印一个值。相反,您需要一种返回到win=0行的机制。一种选择是将代码包装在函数中,然后在答案 == 'y' 时让函数调用自身。

例如

import random
def run_simulation():
win = 0
lose = 0
switch = int(input("Stay or switch? (0-stay/1-switch):")) == 1
n = 0
while n < 100000:
door_values = [0, 0, 0]
removable_doors = [0, 1, 2]
available_doors = [0, 1, 2]
car_place = random.randint(0, 2)
door_values[car_place] = 1
removable_doors.remove(car_place)
door_chosen = random.randint(0, 2)
available_doors.remove(door_chosen)
if door_chosen != car_place:
removable_doors.remove(door_chosen)
door_removed_by_host = removable_doors[random.randint(0, len(removable_doors)-1)]
available_doors.remove(door_removed_by_host)
if switch:
assert(len(available_doors) == 1)
door_chosen = available_doors[0]
if car_place == door_chosen:
win += 1
else:
lose += 1
n+=1
print('win=%s'%str(win))
print('lose=%s'%str(lose))
print("This is the number of wins if the user switched", 
round((win)/1000), "%")
print("This is the number of wins if the user didn't switch", 
round((lose)/1000),"%")
answer = input("Play again? (y/n): ")
if answer == 'y':
run_simulation()
elif answer == 'n':
print("Game Over")
run_simulation()

最新更新