omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Loops

    Pythonista
    3
    4
    2666
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • reefboy
      reefboy last edited by

      How can I loop my commands over and over again

      1 Reply Last reply Reply Quote 0
      • cook
        cook last edited by

        @reefboy hello!

        Using for or while
        read more

        If you mean something really different than that, please let us know!

        1 Reply Last reply Reply Quote 0
        • reefboy
          reefboy last edited by

          @cook yes but may I have an example please? I'm new to programming.

          1 Reply Last reply Reply Quote 0
          • dgelessus
            dgelessus last edited by

            for loops are used to go over items stored in a list and repeat a piece of code for each item. For example, this code:

            mylist = ["hello", "there", "how", "are", "you"]
            
            for message in mylist:
                print message
            

            creates a list named mylist that contains a few strings, then it goes over the list and prints out every item.

            while loops are a little different. They work like an if block that repeats over and over until the condition is false. For example, this code:

            import random
            
            response = ""
            while response != "exit":
                print "Your random number is:", random.randint(0, 10)
                response = raw_input()
            

            prints out a new random number every time you press Enter, until you type exit.

            (By the way, I'm assuming that you're using Python 2. If you're using Python 3, the code won't work like this.)

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors