Home > database >  How can i loop a portion of this python code?
How can i loop a portion of this python code?

Time:09-24

this is my python script, i would like it to loop after an action has been done... Here:

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, The code should loop from 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_______________________________________")

Loop to POINT1 once this is executed

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

Loop to POINT1 once this is executed

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--------------------------------------------------------------------------------------")

Loop to POINT1 once this is executed

NOTE THAT THE 4 SEPARATED CODE PART FOLLOW EACH OTHER IN ONE PIECE

It would be so cool if someone actually managed to help me, i just started learning python and i really would like to finish this project :)

CodePudding user response:

while and for loops in python are what you need.

The syntax of while is as follows:

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!")

As long as the condition is met (while the condition evaluates to True), the loop will continue to execute.

The for loop is a bit different. It doesn't require a condition, but rather a sequence, a collection of sort to enumerate and iterate through.

The syntax of for is as follows:

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!")

Using this new knowledge, you can now use loops where you want and need them in your code.

CodePudding user response:

You will need a while loop.

You will need to put the function that need to put in that loop, separate from the main process of the script.

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()
  • Related