我正在学习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
建议: