【问题标题】:Can anybody tell me where the logic error is for this code please?谁能告诉我这段代码的逻辑错误在哪里?
【发布时间】:2022-01-21 16:40:37
【问题描述】:

这是一段代码,当我运行代码时,当输出应该是骰子总数时,什么都没有输出。有人可以帮我识别错误

import random
r1 = random.randint(1,6)
r2 = random.randint(1,6)
r3 = random.randint(1,6)


def ScoreDice(Dice1 , Dice2 , Dice3):
    Dice1 = r1
    Dice2 = r2
    Dice3 = r3

    if  Dice1 == Dice2 == Dice3:
        total = Dice1 + Dice2 +Dice3
        print(total)
    elif Dice1 == Dice2:
        total = Dice1 + Dice2 - Dice3
        print(total)
    elif Dice2 == Dice3:
        total = Dice3 + Dice2 - Dice1
        print(total)
    elif Dice1 == Dice3:
        total = Dice3 + Dice1 - Dice2
        print(total)
    else:
        total = 0
        print(total)

【问题讨论】:

  • 代码定义了一个函数,但从不调用它...

标签: python error-handling logic logic-error


【解决方案1】:
import random
r1 = random.randint(1,6)
r2 = random.randint(1,6)
r3 = random.randint(1,6)


def ScoreDice(Dice1 , Dice2 , Dice3):

    if  Dice1 == Dice2 == Dice3:
        total = Dice1 + Dice2 +Dice3
        print(total)
    elif Dice1 == Dice2:
        total = Dice1 + Dice2 - Dice3
        print(total)
    elif Dice2 == Dice3:
        total = Dice3 + Dice2 - Dice1
        print(total)
    elif Dice1 == Dice3:
        total = Dice3 + Dice1 - Dice2
        print(total)
    else:
        total = 0
        print(total)

ScoreDice(r1,r2,r3)

我猜你是编码新手......我建议你先研究逻辑和语法,然后再发布你的问题。

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多