【发布时间】:2018-12-18 18:13:33
【问题描述】:
我正在尝试创建一个游戏,您可以在其中选择有多少玩家,命名这些玩家,然后随机选择一名玩家。
从那里,被选中的玩家会在 1 到 10 之间选择一个他们不想登陆的数字。
然后,掷骰子,如果他们落在那个数字上,其他人都可以为他/她选择一个敢。如果没有,游戏只是随机选择另一个玩家,然后重新开始这个过程。
但问题是,程序没有通过询问您不想登陆哪个号码的部分。这很奇怪,因为它适用于一个人。
这是完整的代码,你正在寻找第 23 行:
# Pass Or Dare
from random import randint
firsttry = True
def playerpicked(list):
listwords = len(list)
numpicked = randint(0, listwords - 1)
userpicked = list[numpicked]
return userpicked
while firsttry:
try:
playercount = int(input('How many players will there be?\n'))
except ValueError:
print("That isn't a number. Please enter a number without decimals.")
else:
firsttry = False
playernames = [input("Name of Player {}: ".format(i)) for i in range(1, playercount + 1)]
while True:
playerturn = playerpicked(playernames)
print("The Player picked is:",playerturn)
while True:
try:
darenum = int(input(playerturn + ", which number do you NOT want to land on?\n"))
except ValueError:
print("Please enter a number.")
else:
if darenum > 10 or darenum < 1:
print("Please enter a number between 1 and 10.\n")
else:
break
print("Okay. Rolling the dice...")
numpick = randint(1, 10)
print("The number chosen is " + str(numpick) + ".")
if numpick == darenum:
print("Whoops! The number you chose was the one that we landed on! Everyone agree on a dare for " + playerturn + "!\n\n")
input("Press Enter once " + playerturn + " has done a dare...\n")
else:
print(playerturn + " has escaped! Moving on to the next person.\n\n")
【问题讨论】: