合并两个while循环

2024-03-29 14:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个while循环,它们占用了我代码中的大量空间。我想知道是否有人知道如何合并它们,使我的代码更整洁和更小,因为我甚至不知道从哪里开始。我环顾四周,找不到如何合并两个几乎相同的循环。希望有人能帮我。我发现了一个非常类似的问题,但对我来说,答案没有意义,因为代码不工作,我试图修复它。这是密码。你知道吗

for i in range(5):
while True:
    print(" ")
    input("Press enter to roll player 1 ")
    print("Rolling dice!")
    roll1=random.randint(1,6)
    roll2=random.randint(1,6)
    print(roll1)
    print(roll2)
    total=(roll1 + roll2)
    print("Your total is:" ,total)
    score1=score1 +total

    if roll1 is roll2:
        if total in even:
            score1=score1 +10
            print(score1)
            print("You rolled a double. Roll again.")
            continue
        elif total in odd:
            score1-score1 -5
            print(score1)
            print("You rolled a double. Roll again.")
            continue

    elif total in even:
        score1=score1 +10
        print(score1)
        break

    elif total in odd:
        score1=score1 -5
        print(score1)
        break

if score1<0:
    print("Player 1, you went under 0. Game over.")
    break

while True:
    print(" ")
    input("Press enter to roll player 2 ")
    print("Rolling dice!")
    roll1=random.randint(1,6)
    roll2=random.randint(1,6)
    print(roll1)
    print(roll2)
    total=(roll1 + roll2)
    print("Your total is:" ,total)
    score2=score2 +total

    if roll1 is roll2:
        if total in even:
            score2=score2 +10
            print(score2)
            print("You rolled a double. Roll again.")
            continue
        elif total in odd:
            score2=score2 -5
            print(score2)
            print("You rolled a double. Roll again.")
            continue

    elif total in even:
        score2=score2 +10
        print(score2)
        break

    elif total in odd:
        score2=score2 -5
        print(score2)
        break

if score2<0:
    print("Player 2, you went under 0. Game over.")
    break

正如你所看到的,他们实际上是完全相同的除了球员1到球员2和得分1到得分2。如何简化此代码?你知道吗


Tags: 代码inifisrandomtotalevenprint
3条回答

要避免重复相同的代码,可以做的一件事是定义一个函数,该函数接受玩家的号码并相应地修改该玩家的分数,如updateScore()changeScore()。参数是玩家的数量(1或2)以及你想改变多少分。你知道吗

为该循环创建定义

def roller(player_num):
    while True:
        print(" ")
        input("Press enter to roll player %s " % str(player_num))
        print("Rolling dice!")
        roll1=random.randint(1,6)
        roll2=random.randint(1,6)
        print(roll1)
        print(roll2)
        total=(roll1 + roll2)
        print("Your total is:" ,total)
        score1=score1 +total

        if roll1 is roll2:
            if total in even:
                score1=score1 +10
                print(score1)
                print("You rolled a double. Roll again.")
                continue
            elif total in odd:
                score1-score1 -5
                print(score1)
                print("You rolled a double. Roll again.")
                continue

        elif total in even:
            score1=score1 +10
            print(score1)
            break

        elif total in odd:
            score1=score1 -5
            print(score1)
            break

    if score1<0:
        print("Player %s, you went under 0. Game over." %s str(player_num)
        break

roller(1)
roller(2)

我不知道为什么你会有break语句,因为它会在第一次去打破循环,但这里你有你的代码在定义和它被用于每个用户

对于player1和player2,这个大的while循环实际上是相同的,因此您应该将它设为一个函数,并将Player的名称(或数字)作为参数传递。你知道吗

但还有更多:即使在函数内部,也有很多冗余。例如,检查成对和非成对情况下的滚动是否均匀,可以使用三元... if ... else ...表达式进一步压缩该部分。另外,请注意代码中还有一些问题,例如,您似乎从未初始化score1score2,并且score1-score1 -5中有一个拼写错误。还有,什么是even,所有偶数的列表?相反,可以使用total % 2 == 0检查total是否为偶数。你知道吗

而且,您的if score < 0:检查在循环之外。如果这是有意的,那么break就没有意义了,因为它只会跳出外for循环,这可能不是有意的。此外,您还可以为分数不是< 0的情况添加一些输出。你知道吗

将所有这些放在一起,函数可以如下所示:

def roll(player):
    score = 0
    while True:
        input("\nPress enter to roll, %s" % player)
        roll1 = random.randint(1,6)
        roll2 = random.randint(1,6)
        print("Rolling dice!", roll1, roll2)
        total = roll1 + roll2
        score += total
        score += 10 if total % 2 == 0 else -5
        print("Your total is:", score)
        if roll1 == roll2:
            print("You rolled a double. Roll again.")
        else:
            break
    if score < 0:
        print("%s, you went under 0. Game over." % player)
    else:
        print("%s, your final score is %d." % (player, score))

现在,只需调用该函数两次:

for _ in range(5):
    roll("Player 1")
    roll("Player 2")

相关问题 更多 >