Python的新手-您可以查看我的压模程序吗? [关闭]

问题描述 投票:0回答:1

我正在学习python,因此为了学习,我制作了一个压模应用程序。你将做点什么不同的?您对提高性能/可读性有何建议?你有什么技巧?建议几乎任何东西。

import random
import time
import re
#installed via pip install inflect (https://pypi.org/project/inflect/) 
import inflect

while True:
    confirmation = input("Would you like to roll some dice?(y/n) > ")

    if len(confirmation) != 1:
        print("Error! You may only input one character")

    elif not re.match(r"^[yn]$", confirmation, flags=re.IGNORECASE): 
        print("Error! Only y and n are valid")

    else :
        break

if confirmation.casefold() == "n" :
    print("No dice have been rolled")
elif confirmation.casefold() == "y":
    while True:
        count = input("How many dice would you like to roll? > ")

        if len(count) == 0 :
            print("Error! Please input something!")

        elif not re.search(r"^[0-9]*$", count) :
            print("Error! You may only input numbers")

        else :
            p = inflect.engine()
            for i in range(1, int(count)+1) :
                result = random.randint(1,6)
                print("The " + p.number_to_words(p.ordinal(i)) + " die rolled a: " + str(result))

            break
python python-3.x
1个回答
0
投票

建议:

  1. 除了输入完整的字符串,您只能从控制台读取第一个字符。看起来会更好。
  2. 最后,您可以要求用户确认是否退出程序或再次滚动。
© www.soinside.com 2019 - 2024. All rights reserved.