【发布时间】:2020-10-31 18:14:39
【问题描述】:
这是我的代码:
import random
import keyboard
print("First, press enter to randomize your first mastery:")
while True:
if keyboard.is_pressed("enter"):
with open("masteries.txt", "r") as f:
masteries = f.read().splitlines()
random_mastery1 = random.choice(masteries)
break
file_name = random_mastery1.lower() + "_skills.txt"
print(f"Next, press enter to randomize your primary active skill for your first randomized mastery:")
while True:
if keyboard.is_pressed("enter"):
with open(file_name, "r") as skill_file:
skills = skill_file.read().splitlines()
random_skill = random.choice(skills)
break
print(f"Finally, press enter to randomize your second mastery:")
while True:
if keyboard.is_pressed("enter"):
with open("masteries.txt", "r") as f:
masteries = f.read().splitlines()
random_mastery2 = random.choice(masteries)
break
print(f"You'll be starting as {random_mastery1} with a primary active skill of {random_skill}." +
f" Your second mastery will be {random_mastery2}. Have fun!")
该程序在核心功能方面完全符合我的要求,但是在第一次按 Enter 以随机化第一个掌握之后,它不会中断并等待接下来的两个输入输入,它只是运行并在之后完成按回车一次。示例:
First, press enter to randomize your first mastery:
**I press enter here**
Next, press enter to randomize your primary active skill for your first randomized mastery:
Finally, press enter to randomize your second mastery:
You'll be starting as Shaman with a primary active skill of Grasping Vines. Your second mastery will be Nightblade. Have fun!
关于如何解决这个问题或更好地实现我想要完成的任何想法?
【问题讨论】:
标签: python while-loop break