【问题标题】:How can i loop a portion of this python code?我怎样才能循环这个python代码的一部分?
【发布时间】:2021-09-23 17:58:40
【问题描述】:

这是我的 python 脚本,我希望它在一个动作完成后循环...... 这里:

print("Booting System (Alfa V1.1)")
time.sleep(1)
print("Hello, and welcome to Training OS")
time.sleep(2)
print("What would you like to do ?")
time.sleep(1)

POINT1 HERE,代码应该从这里循环

txt = raw_input("1.File Explorer  2.System Info.  ( 1 or 2 )")
if txt=="1": 
    print("Loading File Explorer") 
    time.sleep(1)
    print("  _____    _____ \n /     |  /     | \n |     |  |     | \n |_____|  |_____| \n  File1    File2  ")
    time.sleep(0.5)
    file = raw_input("1.File1 2.File2 ( 1 or 2 )")
    if file=="1":
        print("\n\nFile1:"
              "\n_______________________________________"
              "\nTOP SECRET UPDATE INFO:"
              "\n           Update Name . . . . 1.2"
              "\nEstimated Release Date . . . . 25.09.21"
              "\n           New Features . . . . Games?"
              "\n_______________________________________")

执行后循环到 POINT1

    else:
        print("\n\nFile2:"
              "\n----------------------------------------"
              "\n                                        "
              "\n              Error 404.                "
              "\n             File Missing               "
              "\n                                        "
              "\n----------------------------------------")

执行后循环到 POINT1

else:
    print("\n\nSystem Info:"
          "\n--------------------------------------------------------------------------------------"
          "\n\nOs version: . . . . . 1.1 (New features: File Explorer)"
          "\n   Founded: . . . . . 22.09.2021, Baku, Azerbaijan"
          "\n  Products: . . . . . TOS"
          "\n\n***This OS is still being developed so some features are currently unaviable :(***"
          "\n--------------------------------------------------------------------------------------")

执行后循环到 POINT1

请注意,4 个单独的代码部分彼此相连

如果有人真的能帮助我,那就太酷了,我刚开始学习 python,我真的很想完成这个项目 :)

【问题讨论】:

  • 顺便说一句,您使用的似乎是python 2,它已经报废了。 raw_input 在 python 3 中更改为 input

标签: python loops input


【解决方案1】:

whilefor 在 python 中的循环是你所需要的。

while的语法如下:

while condition:
    # This section will continue to execute until the condition declared above is False.
    do_something()
    some_var = "Value"
    do_something_crazy(some_var)

# This section will be executed after the loop has finished.
print("Wow very cool!")

只要满足条件(条件为真),循环就会继续执行。

for 循环有点不同。它不需要条件,而是需要一个序列,一个要枚举和迭代的排序集合。

for的语法如下:

for variable in collection:
    # This section will execute the for each item in the collection. In each iteration the value of `variable` will be of the current item in the collection.
    do_something_with(variable)
    print(variable, "is very cool")

# This section will execute after the last item in the collection.
print("These are all the items")

# Example:
cool_dogs = ["Shiba Inu", "Bulldog", "Pitbull"]

for dog in cool_dogs:
    print("This dog is one of my favorites: ", dog)

print("I love dogs!")

利用这些新知识,您现在可以在代码中需要和需要的地方使用循环。

【讨论】:

  • 谢谢,我会再读几次以了解代码以及如何在我的脚本中使用它,谢谢您的帮助:)
【解决方案2】:

你需要一个while循环。

您需要将需要放入该循环的函数与脚本的主进程分开。

def ask_or_something():
    txt = raw_input("1.File Explorer  2.System Info.  ( 1 or 2 )")
    if txt == "1":
        print("Loading File Explorer")
        time.sleep(1)
        print("  _____    _____ \n /     |  /     | \n |     |  |     | \n |_____|  |_____| \n  File1    File2  ")
        time.sleep(0.5)
        file = raw_input("1.File1 2.File2 ( 1 or 2 )")
        if file == "1":
            print("\n\nFile1:"
                  "\n_______________________________________"
                  "\nTOP SECRET UPDATE INFO:"
                  "\n           Update Name . . . . 1.2"
                  "\nEstimated Release Date . . . . 25.09.21"
                  "\n           New Features . . . . Games?"
                  "\n_______________________________________")

        else:
            print("\n\nFile2:"
                  "\n----------------------------------------"
                  "\n                                        "
                  "\n              Error 404.                "
                  "\n             File Missing               "
                  "\n                                        "
                  "\n----------------------------------------")

    else:
        print("\n\nSystem Info:"
              "\n--------------------------------------------------------------------------------------"
              "\n\nOs version: . . . . . 1.1 (New features: File Explorer)"
              "\n   Founded: . . . . . 22.09.2021, Baku, Azerbaijan"
              "\n  Products: . . . . . TOS"
              "\n\n***This OS is still being developed so some features are currently unaviable :(***"
              "\n--------------------------------------------------------------------------------------")
        
# need a main process to control all the loop
if __name__ == '__main__':
    while True:
        ask_or_something()

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多