Preview

10 -Intro to Loops

 1. Iteration is the word we use to refer to the use of loops in programming. Which of the following is NOT a type of loop. (select from the code examples #1, #2 and #3 below)
#1
for friend in ['Margot', 'Kathryn', 'Prisila']:
    invitation = "Hi " + friend + ".  Please come to my party on Saturday!"
    print(invitation)




#2 
food = 'spam'

if food == 'spam':
    print('Ummmm, my favorite!')
    print('I feel like saying it 100 times...')
    print(100 * (food + '! '))
	
	
#3
number = 0
prompt = "What is the meaning of life, the universe, and everything? "

while number != "42":
    number =  input(prompt)

  All of the examples #1, #2 and #3 demonstrate the use of loops

  #2

  #1

  #3

 2. Repeating identical or similar tasks without making errors is something that computers do well and *most* people do poorly. What is happening in the code below?
>>> for i in range(5):
...     print('i is now:', i)
...
i is now 0
i is now 1
i is now 2
i is now 3
i is now 4
>>>

  the computer is printing the value for the variable i, starting at 0, going up 5

  the computer is checking to see if the variable i is equal to 0

  the computer is trying to add the numbers 0 to 5

  the computer is looking up the value of i for every number between 0 to 5

 3. Code snippet #1 and Code snippet #2 below produce the same output of 1,2,3,4,5. Which statement is correct?
#Code snippet 1

print(1)
print(2)
print(3)
print(4)
print(5)


#Code snippet 2

for i in range(1,6):
  print(i)

  The first produces 1,2,3,4,5 but the second produces a timestable program

  No, the first outputs 1,2,3,4,5 but the second outputs 1,2,3,4,5,6

  Yes. Both produce the same output of 1,2,3,4,5

  No, both programs print out 1,2,3,4,5,6

 4. How is a while loop different from a for loop, with particular reference to line 12 in the while loop below? (note in a for loop, it automatically goes up from 0 to the top range specified, which in this case is 11)
#this is a for loop that prints 1,2,3,4,5,6,7,8,9,10

for i in range(11):
  print(i)
  
  
#this a while loop that also does the same thing

i = 0 
while i <11:
  print(i)
  i=i+1

  The while loop needs to do some addition in order to carry on

  The while loop requires incrementation (to go up to meet the stopping condition)

  The while loop is always equivalent to 1+1

  The while loop goes on forever

 5. In the for loop below, it prints the numbers 1,2,3,4. What one thing do you need to change on line 1to make it print numbers 1,2,3,4,5?
for i in range(1,5):
  print(i)

  for i in range(1,6):

  for i in range(1,2,3,4,5,6)

  for i in range(please print 6 as well python)

  for i in range(6)

 6. What type of loop has been used below and where is the stopping condition for the while loop defined?
name = 'Jonathan'
guess = input("So I'm thinking of person's name. Try to guess it: ")
pos = 0

while guess != name and pos < len(name):
    print("Nope, that's not it! Hint: letter ", end='')
    print(pos + 1, "is", name[pos] + ". ", end='')
    guess = input("Guess again: ")
    pos = pos + 1

if pos == len(name) and name != guess:
    print("Too bad, you couldn't get it.  The name was", name + ".")
else:
    print("\nGreat, you got it in", pos + 1,  "guesses!")

  For Loop. Line 5

  While loop. Line 5

  If Loop. Line 2

  While Loop. Line 11

 7. The following program uses a for loop in line 5 to limit the user's tries (to enter their password) to three attempts. Is there a mistake on line 5?
def main():

    password=input("What is your password : ")

    for i in range(2): 
        if password=="open123":
            print("yes, that's it ...")
            accessgranted(
        else:
            print("try again")
            password=input("Enter password again: ")
    triesover()

        
def accessgranted():
    print("****ACCESS GRANTED *****")
    

def triesover():
    print("TOO MANY TRIES, SORRY!")
    
main()

  Yes. for i in range(2) should be for i in range(3) to allow three attempts

  No. for i in range(2) will allow three attempts, because it starts at 0 - - 1 - - -2

  No. for i in range(2) is correct because 2+1 is equal to 3

  Yes. for i in range(2) should be for 3 in range(3)

 8. What will the following program do?
names =['Abraham', 'Issac', 'Jacob', 'Judah']
for n in names:
    print(n)

  It will print all the names in the list - Abraham, Issac, Jacob and Judah

  It will print the first item in the list: Abraham

  It will print the last item in the list: Judah

  It will print the letter 'n' as the program has not been set up correctly.

 9. The three types of loops we typically learn about in programming are the For loop, the while loop and the repeat - until loop. In the repeat - until loop, the stopping condition is at the end. In the for loop, the start and stop condition is predefined at

  the stopping condition is at the start

  there is no stopping condition

  the stopping condition is at the end

  the stopping condition is in the middle

 10. In the code below that makes use of a for loop, what would the first two lines of output be, if the user entered the number 3 on line 2.
def main():
    number=int(input("Enter a number : "))
    
    for i in range(0,21):
        print(number,"times:", i, "is equal to:", i*number)        
        
main()

  Error - there would be no output

  3 times: 0 is equal to: 0 and 3 times: 1 is equal to: 3

  number: 3 and number: 6

  number,"times:", i, and 3