"Timed out while waiting for the program to exit" - CS50 图



我刚上完第六周的课,正在做练习题。我被困在FIGlet,我不明白为什么我得到超时错误消息与check50:/

check50结果

from pyfiglet import Figlet
from sys import argv
import sys
import random
figlet = Figlet()
string = input("Input: ")
# the user would like to output text in a random font.
if len(sys.argv) == 1:
figlet.setFont(font=random.choice(font_list))
print(f"Output: {figlet.renderText(string)}")
# the user would like to output text in a specific font
elif len(sys.argv) == 3 and (argv[1] == "-f" or argv[1] == "--font"):
if argv[2] in figlet.getFonts():
figlet.setFont(font=argv[2])
print(f"Output: {figlet.renderText(string)}")
else:
sys.exit("Invalid usage")
# otherwise error
else:
sys.exit("Invalid usage")
当我做测试时,

程序按预期工作。你能帮我一下吗?这只是我对python的第二次尝试,所以如果你也有关于如何使代码更好的技巧,我将不胜感激!

当参数无效时不要要求输入。首先检查参数,然后请求输入并在最后打印结果。

from pyfiglet import Figlet
from sys import argv
import sys
import random
figlet = Figlet()
# the user would like to output text in a random font.
if len(sys.argv) == 1:
figlet.setFont(font=random.choice(font_list))
# the user would like to output text in a specific font
elif len(sys.argv) == 3 and (argv[1] == "-f" or argv[1] == "--font"):
if argv[2] in figlet.getFonts():
figlet.setFont(font=argv[2])
else:
sys.exit("Invalid usage")
# otherwise error
else:
sys.exit("Invalid usage")
string = input("Input: ")
print(f"Output: {figlet.renderText(string)}")

超时,因为check50在给出无效参数时没有提供任何输入,所以脚本将永远等待。

相关内容

  • 没有找到相关文章

最新更新