Previous Lecture Lec 9 Next Lecture

Lec 9, Mon 02/03

while loops, random module

HW05 and HW06

Survey

What we will be reviewing today:

pytest function skeleton:

def test_function_XXX():
    assert functino(X) == return_value
    
def test_function_YYY():
	assert_function(Y) == return_value

“Helper” functions

random module

print (Random, random.randint(25, 50))

Running the following code multiple times outputs seemingly random numbers:

import random
print("Random", random.randint(1, 10))

outputs

Random 10

Random 7

Random 1

The number really is random! We get a different output everytime.

Number-guessing game with the computer

Debugging technique alert: Note that at every step, we’ll be outputting the values to see what is happening in our program and to verify that it matches what we expect.

the_num = random.randint(1,10)
print("Computer selected ", the_num) # Verify that the computer is giving us the ouput we expect.
guess = input("Select a number between 0 and 10: ") # Asking the user for a number.
print("You selected: ", guess) # Printing the input back to the user

Output:

Computer selected  6
Select a number between 0 and 10: 7
You selected:  7

What might be a potential problem?

How do we do this? Define two variables start_num and end_num and use them throughout our code.

start_num = 1
end_num = 10
the_num = random.randint(start_num, end_num)
print("Computer selected ", the_num)

prompt = "Select a number between " + str(start_num)
prompt += " and " + str(end_num)
'''
Here we take start_num and end_num and concatenate them into the string, but we have to
convert the number into a string first so python doesn't give us a TypeError!
We use += to add the updated value of prompt to end_num 
''' 

guess = input(prompt)
print("You selected: ", guess)

Output:

Computer selected  9
Select a number between 1 and 102
You selected:  2

Now we will try to convert the computer’s ouput into a string so it matches the type of the user input.

Check Monday’s lecture notes to see code for converting the user’s input into an int so it matches the computer’s output.

Pseudo code:

- Convert the_num into str
- While the user did not guess correctly
- Check if the user's guess is the same as the_num
    - If True (they are the same) 
        - Print "You got it!"
    - Else
        - Ask the user for another guess
the_num = str(the_num) # Overwriting the old value of the_num
guess = None
'''
Notice that we have to create this variable before the while loop begins so it 
exists when the function ends.
'''

while guess != the_num:
    guess = input(prompt) 
    print ("You selected: ", guess)
    
    if guess == the_num:
        print ("You got it!")
        break
    '''
    Don't need to include an else because if guess does not equal the_num, 
    we will loop through the while loop again
    
    Break will get us out of the while loop, don't want to check equality again
    '''
print ("The End")
    

Running the code:

Select a number between 1 and 109
You selected:  9
Select a number between 1 and 104
You selected:  4
Select a number between 1 and 107
You selected:  7
Select a number between 1 and 103
You selected:  3

What is the difference between break and continue?

i = 0
break_num = 8

print("Break in action")
print("Stop after", break_num)

while i <11:
    if i == break_num:
        break
    print(i)
    i += 1

print("Continue in action")
i = 0
divisible_by = 3

while i < 10:
    if i % divisible_by == 0: #Checks if i is divisible by our variable, divisible_by, by checking if the remainder is 0
        print (i, " is divisible by ", divisible_by)
        i += 1 # Without this line, we would get stuck in an infinite loop
        continue
    print(i) # Notice there is an implicit else here
    i += 1

print ("Done")

Output:

Break in action
Stop after 8
0
1
2
3
4
5
6
7
Continue in action
0,  is divisible by  3
1
2
3  is divisible by  3
4
5
6  is divisible by  3
7
8
9  is divisible by  3
Done

Is pass the same as break?

print("Pass in action")
i = 0
divisible_by = 3

while i < 10:
    if i % divisible_by == 0: #Checks if i is divisible by our variable, divisible_by, by checking if the remainder is 0
        print (i, " is divisible by ", divisible_by)
        i += 1 # Without this line, we would get stuck in an infinite loop
        pass
    print(i) # Notice there is an implicit else here
    i += 1

print ("Done")
Pass in action
0  is divisible by  3
1
2
3  is divisible by  3
4
5
6  is divisible by  3
7
8
9  is divisible by  3
10
Done

When you see a pass, this is usually an indicator that something is not yet inplemented, or you can rewrite your code without using the pass.

We have now seen pass, continue, and break in action. These allow us to redirect the flow of execution.

Let’s now try asking the user if they want to keep going after the function ends.

answer = 'y' # The user's input won't be a number anymore, now it will be a yes or no

while guess != the_num:
    guess = input(prompt) 
    print ("You selected: ", guess)
    
    if guess == the_num:
        print ("You got it!")
        answer = input("Do you want to continue? ")
        if answer == 'y':
            the_num = str(random.radnint(start_num, end_num))
print ("The End")
Select a number between 1 and 103
You selected:  3
Select a number between 1 and 106
You selected:  6
Select a number between 1 and 109
You selected:  9
Select a number between 1 and 103
You selected:  3
import random
print ("Random", random.randint(25,50))
# print ("Random", random.randint(25,50))
first_num = 1
last_num = 10
the_num = random.randint(first_num, last_num)

prompt = "Select a num between " + str(first_num)
###print(prompt)
#prompt = prompt + " and " + str(last_num)
prompt += " and " + str(last_num) + ": "
###print(prompt)

while guess != the _num:
  guess = input(prompt)
  guess = int(guess)
  # check if user's guess is the_num
  if guess == the_num:
    print("Yes!! Winner!!")
  else:
    print("Guess again")
print("The End")