有没有更干净的方式编写此代码? D&D骰子辊

时间:2019-06-12 08:34:26

标签: python python-3.x

我觉得这非常简单并且易于阅读,但是我想知道是否有一种简单或更有效的方式来编写此代码。 这是我第一个项目的开始。

import random

command = "".lower()

def dice():
    while True:
        dice_entry = input(":> ").lower().strip()
        if dice_entry == "help":
            print("""
Valid commands include:
Coin, d4, d6, d8, d10, d12 or d20
in order to roll the corresponding dice
or flip a coin.

Type Quit to close program.""")

        elif dice_entry == "coin":
            print("".join(random.choices(['Heads', 'Tails'])))

        elif dice_entry == "d4":
            print(random.randint(1, 4))

        elif dice_entry == "d6":
            print(random.randint(1, 6))

        elif dice_entry == "d8":
            print(random.randint(1, 8))

        elif dice_entry == "d10":
            print(random.randint(1, 10))

        elif dice_entry == "d12":
            print(random.randint(1, 12))

        elif dice_entry == "d20":
            print(random.randint(1, 20))

        elif dice_entry == "quit":
            break
        else:
            print("Invalid command. Type \"Help\" for list of valid commands.")

2 个答案:

答案 0 :(得分:2)

肯定有:

变量的命名:您的变量dice_entry的名称不正确。您还可以扔硬币,退出或键入与骰子无关的帮助。

使用random.choices:您可以改用random.choice,而避免使用"".join

WET:某些代码被重复多次,您可以使用一个函数。

无用的代码:命令变量的意义是什么?特别是"".lower()的意义是什么?为什么不只是""

import random

def throw_dice(nb_faces):
    return random.randint(1, nb_faces)

def throw_coin():
    return random.choice(['Heads', 'Tails'])

def main():
    while True:
        entry = input(":> ").lower().strip()

        if entry == "help":
            print("""
Valid commands include:
Coin, d4, d6, d8, d10, d12 or d20
in order to roll the corresponding dice
or flip a coin.

Type Quit to close program.""")

        elif entry == "coin":
            print(throw_coin())

        elif entry in ["d4", "d6", "d8", "d10", "d12", "d20"]:
            nb_faces = int(entry[1:])
            print(throw_dice(nb_faces))

        elif entry == "quit":
            break

        else:
            print('Invalid command. Type "Help" for list of valid commands.')

答案 1 :(得分:2)

一种选择是使用寻址字典:

import random

def dice():
    help_message = (
        "Valid commands include:\n"
        "Coin, d4, d6, d8, d10, d12 or d20 in order to roll the corresponding dice or flip a coin.\n"
        "Type Quit to close program.\n"
    )
    commands_dict = {
        "help": lambda: help_message,
        "coin": lambda: random.choice(("Heads", "Tails")),
        "d4"  : lambda: random.randint(1,  4),
        "d8"  : lambda: random.randint(1,  8),
        "d10" : lambda: random.randint(1, 10),
        "d12" : lambda: random.randint(1, 12),
        "d20" : lambda: random.randint(1, 20)
    }
    default = lambda: "Invalid command. Type \"Help\" for list of valid commands."
    while True:
        dice_entry = input(":> ").lower().strip()
        if dice_entry == "quit":
            break
        print(commands_dict.get(dice_entry, default)())

这里有live example