骰子游戏不适用于多个玩家

时间:2018-12-18 18:13:34

标签: python dice

我正在尝试创建一个游戏,您在其中选择将要有多少个玩家,为这些玩家命名,然后随机选择一个玩家。

从那里,所选的玩家会在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") 

2 个答案:

答案 0 :(得分:0)

这里是对该算法的快速回顾,问题是您在检查darenumber是否在所需范围之间时使用了中断,从而导致再次循环并永远不会离开那里。 另外,建议您检查控件结构,因为这就是问题所在。 最后,我强烈建议您退出游戏,因为在某些时候必须退出游戏。希望对您有所帮助:

from random import randint

def playerpicked(list):
    listwords = len(list)
    numpicked = randint(0, listwords - 1)
    userpicked = list[numpicked]
    return userpicked

playercount = 0
while playercount < 1:
    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.")

playernames = [input("Name of Player {}: ".format(i)) for i in range(1, playercount + 1)]
print("Press 0 while it's your turn to finish the game")
while True:
    playerturn = playerpicked(playernames)
    print("The Player picked is:",playerturn)
    darenum = -1
    while (darenum > 10 or darenum < 0):
        try:
            darenum = int(input(playerturn + ", which number do you NOT want to land on?\n"))
            if darenum > 10 or darenum < 1:
                print("Please enter a number between 1 and 10.\n")
        except ValueError:
            print("Please enter a number.")
    if darenum == 0:
        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") 

print("Game Over")

答案 1 :(得分:-1)

您的原始代码,带有错误结束符(请阅读代码的注释):

# 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: #there is no if for this else
                    if darenum > 10 or darenum < 1:
                        print("Please enter a number between 1 and 10.\n")
                    else:
                        break #this break would break the while True above
                    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") 
                        #there should be a break in here, otherwise the function would be stuck in the second while True

固定代码,更改了我在上面的注释中提到的内容:

# 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.")

                if darenum > 10 or darenum < 1:
                    print("Please enter a number between 1 and 10.\n")

                else:

                    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")
                        break