【问题标题】:IDLE - Python: Go back to a certain line, how can I do this?IDLE - Python:回到某一行,我该怎么做?
【发布时间】:2012-12-28 18:37:11
【问题描述】:

我创建了一个选择菜单,要求您输入要运行的命令,我已经使用 ifelif 语句完成了此操作,但是当命令(if 语句)完成时,我会喜欢他们去询问要运行哪个命令的行。这是我目前拥有的代码(现在我没有这样做):

# 询问您要运行哪个命令。 word = input("哪个命令?") # 运行命令/s。 如果单词 == “信息”: print("信息命令。") elif 字 == “替换”: print("替换命令") elif 字 == “平”: print("Ping 命令") 别的: print("找不到命令")

如果有人能帮忙就太好了,谢谢。

【问题讨论】:

  • 将所有内容放在while True 循环中。您可以找到 while 在网络中如何工作的示例

标签: python if-statement python-idle


【解决方案1】:

对不起,如果这太多了,但您可能需要考虑将其放入函数中以执行以下操作:

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass

这将与while True 然后if something: break 相同

【讨论】:

    【解决方案2】:

    尝试将您的代码包装在 while True: 块中。这将无限期地重复代码。

    如需进一步阅读,请尝试this

    【讨论】:

    • 当:真?这是 Python 吗?
    猜你喜欢
    • 2018-01-16
    • 2012-05-26
    • 1970-01-01
    • 2014-01-23
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多