1 / 7

Python - Iteration Iteration

Python - Iteration Iteration Iteration is a posh way of saying “loop” (iteration literally means to do something again ). Loops are absolutely vital in programming in order to avoid having to write sequences of code out again and again. And they come in several forms:. Python - Iteration

orea
Download Presentation

Python - Iteration Iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python - Iteration Iteration Iteration is a posh way of saying “loop” (iteration literally means to do something again). Loops are absolutely vital in programming in order to avoid having to write sequences of code out again and again. And they come in several forms:

  2. Python - Iteration While Loops A WHILE loop is a way of repeating a section of code. It is easy to understand because it uses something that looks a lot like an IF statement at the top.

  3. Python - Iteration How many times do you think this loop will repeat? Try it and find out: number = 1 while number < 10: print(“This is turn”,number) number = number + 1 print(“The loop is now finished”) Make sure you understand why the last line only prints out once, at the end of the loop.

  4. Python - Iteration A WHILE loop is ideal if you don’t know exactly how many times you will need to repeat the code.

  5. Python - Iteration # this is the correct number targetNumber = 7 # ask for a guess guess = int(input(“Guess a number between 1 and 10”)) # repeat if the answer is wrong while guess != targetNumber: print(“Wrong, try again”) guess = int(input(“Guess a number between 1 and 10”)) # do this after the loop print(“Congratulations - that’s right!”)

  6. Python - Iteration • Tasks • Create a program that gets two players to enter in their names and then you choose a Gamenumber between 10 and 50. Each player takes it in turns to choose either 1,2 or 3. This number is then deducted from the Gamenumber. The last person to deduct a number from the Gamenumber before it reaches 0 loses.

  7. Python - Iteration Solution – Pseudo Code Get names. Playing = 1 GameNumber = 25 While GameNumber > 0 Get number from user GameNumber – number Show GameNumber if GameNumber > 0 if Playing = 1 Playing = 2 else Playing = 1 If playing = 1 Player 1 loses else Player 2 loses

More Related