我刚开始编程,所以很抱歉这个愚蠢的问题。我编写了一个程序,当用户输入"r"时,它会在2到12之间随机选择一个整数(就像一对六面骰子)。我希望用户能够输入一个小写的" "或者大写的"R"达到同样的结果,但我不知道该怎么做。这同样适用于其余的代码,当用户被询问是否想要使用&;y&;或";n"。此外,我认为整个代码可能比它需要的要长得多。请教我如何优化它并获得相同的结果。
import random
def roll():
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
while roll_dice != "x":
if roll_dice != "r":
roll_dice = input("Invalid input. Try again ")
else:
result = random.randint(2,12)
print(result)
break
return result
def dice():
result = roll()
roll_dice = input("Would you like to roll again? (Y/N) ")
while roll_dice != "n":
if roll_dice == "y":
result = random.randint(2,12)
print(result)
roll_dice = input("Would you like to roll again? (Y/N) ")
else:
roll_dice = input("That is an invalid answer. Please try again ")
print("You have finished rolling")
dice()
当我为你的问题编写答案和解决方案时,我看到已经提出了类似的答案。我仍然会提出一个解决方案的建议,与前面的评论相似,那就是使用"lower()"。字符串函数。您可以找到对其用法的多个引用,但这里有一个可能会引用String小写。
在Python中使用此功能是您的代码的一个可能的重构版本。
import random
def roll():
result = 2
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
while roll_dice.lower() != "x":
if roll_dice.lower() != "r":
roll_dice = input("Invalid input. Try again ")
else:
result = random.randint(2,12)
print(result)
break
return result
def dice():
result = roll()
roll_dice = input("Would you like to roll again? (Y/N) ")
while roll_dice.lower() != "n":
if roll_dice.lower() == "y":
result = random.randint(2,12)
print(result)
roll_dice = input("Would you like to roll again? (Y/N) ")
else:
roll_dice = input("That is an invalid answer. Please try again ")
print("You have finished rolling")
dice()
在终端上测试这些重构的代码产生了以下测试结果:
@Vera:~/Python_Programs/Dice$ python3 Roll.py
Enter R to roll the dice. Enter X to exit r
11
Would you like to roll again? (Y/N) y
6
Would you like to roll again? (Y/N) n
You have finished rolling
所有的答案都是建设性的,应该对你有所帮助。看看这些重构的代码是否符合你项目的精神。
使用一些条件检查
if roll_dice != "r" and roll_dice != "R":
可以在if语句中检查多个条件:
if roll_dice != "r" and roll_dice != "R":
或者您可以将输入转换为小写:
roll_dice = input("Enter R to roll the dice. Enter X to exit ")
roll_dice = roll_dice.lower()
关于改进代码的技巧,我建议如下:
- 使用描述性名称,这样您就可以立即看到某事是什么/做什么,而无需查看逻辑。例如,通过首先为条件指定一个描述性名称来拆分if语句和条件。
- 保持功能紧凑。他们应该只做一件事。 当有
if
语句时,尽量不要使用else
,以防止逻辑嵌套。例如:import random
def get_dice_roll_results(number_of_dice=1, number_of_faces=6):
roll_results = []
for index in range(number_of_dice):
roll_results.append(random.randint(1, number_of_faces))
return roll_results
def run():
total_roll_count = 0
while True:
query = "Would you like to roll the dice? (y/n)n"
is_rolling_again = total_roll_count > 0
if is_rolling_again:
query = "nWould you like to roll the dice again? (y/n)n"
input_value = input(query).lower()
answer_is_invalid = input_value != "y" and input_value != "n"
if answer_is_invalid:
print("That is an invalid answer. Please try again.")
continue
should_role_dice = input_value.lower() == "y"
if not should_role_dice:
break
roll_results = get_dice_roll_results(2, 6)
total_roll_count+=1
result_template = "You rolled ({individual_dice}) totalling {total}!"
formatted_result = result_template.format(
total=sum(roll_results),
individual_dice=", ".join(str(roll) for roll in roll_results)
)
print(formatted_result)
print("nYou have finished rolling!")
run()