160 likes | 269 Views
COMPSCI 101 Principles of Programming. Lecture 24 Random Numbers, User Input and While Loops Dr. Patricia J. Riddle. Learning outcomes. At the end of this lecture, students should be able to: Use random number generator Use while loops Get user input from the keyboard. Review.
E N D
COMPSCI 101Principles of Programming Lecture 24 Random Numbers, User Input and While Loops Dr. Patricia J. Riddle
Learning outcomes • At the end of this lecture, students should be able to: • Use random number generator • Use while loops • Get user input from the keyboard COMPSCI 101 - Principles of Programming
Review random.randint(1,8) • Returns a random integer between 1 and 8 (inclusive) • Must import the random module COMPSCI 101 - Principles of Programming
User Input • input([prompt]) • Displays prompt (if given) and reads a line from keyboard • Returns the line as a string, e.g. • name = input("What is your name? ") • If you want to read numbers, convert the string into a number using int or float as appropriate, e.g. • age_as_string = input("How old are you? ") • age = int(age_as_string) • Or • weight = float(input("What's your weight in kg? ")) COMPSCI 101 - Principles of Programming
While Loop • The for loop iterates over a known sequence. • Sometimes we don’t have a known sequence, e.g. • “Stir until boiling” • “Keep reading input until the user types quit” • “Keep refining the answer until it’s good enough” • Situations like this need a different sort of loop: • The while loop COMPSCI 101 - Principles of Programming
While Example • If a population of bacteria increases in size by 21% every minute, how long does it take for the population size to double? minutes = 0 population = 1000 growth_rate = 0.21 while population < 2000: population *= (1 + growth_rate) minutes += 1 print(minutes, " minutes required.") print("Population =", int(population)) COMPSCI 101 - Principles of Programming
Exercise • Write a program that calls a function function named play_rock_paper() that plays rock paper scissors with the user. User input must be from the keyboard. Example of Game: Choose "r" "p" or "s": r Computer chose rock. You chose rock. We are tied. Do you want to play again? Answer "y" or "n": y Choose "r" "p" or "s": s Computer chose rock. You chose scissors. Computer wins. Do you want to play again? Answer "y" or "n": y Choose "r" "p" or "s": p Computer chose rock. You chose paper. You win. COMPSCI 101 - Principles of Programming
Answer COMPSCI 101 - Principles of Programming
Helper Function • “who_wins” helper function from Slide 5 in Lecture 21 COMPSCI 101 - Principles of Programming
More Nested Loop Practice COMPSCI 101 - Principles of Programming
Exercise • Write a function named test_sudoku()that accepts a list specifying the sudoku board and and returns True if the board is a legal sudoku board and False otherwise. 0 represents an empty cell in the board. Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 sub-grids that compose the grid (also called "boxes", "blocks", "regions", or "sub-squares") contains all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a unique solution. Wikipedia >>> test_sudoku([5, 9, 8, 2, 7, 4, 1, 3, 6, 3, 2, 4, 9, 1, 6, 8, 5, 7, 1, 7, 6, 5, 8, 3, 2, 9, 4, 9, 8, 5, 3, 2, 7, 6, 4, 1, 7, 1, 2, 4, 6, 5, 3, 8, 9, 4, 6, 3, 8, 9, 1, 5, 7, 2, 6, 4, 1, 7, 5, 8, 9, 2, 3, 2, 5, 7, 1, 3, 9, 4, 6, 8, 8, 3, 9, 6, 4, 2, 7, 1, 5]) True >>> test_sudoku([5, 5, 8, 2, 7, 4, 1, 3, 6, 3, 2, 4, 9, 1, 6, 8, 5, 7, 1, 7, 6, 5, 8, 3, 2, 9, 4 , 9, 8, 5, 3, 2, 7, 6, 4, 1, 7, 1, 2, 4, 6, 5, 3, 8, 9, 4, 6, 3, 8, 9, 1, 5, 7, 2, 6, 4, 1, 7, 5, 8, 9, 2, 3, 2, 5, 7, 1, 3, 9, 4, 6, 8, 8, 3, 9, 6, 4, 2, 7, 1, 5]) False COMPSCI 101 - Principles of Programming
Suduko Example COMPSCI 101 - Principles of Programming
Answer COMPSCI 101 - Principles of Programming
Helper Functions COMPSCI 101 - Principles of Programming
Summary • While loops • User input • Random number generation COMPSCI 101 - Principles of Programming
Next Tuesday • Dictionaries COMPSCI 101 - Principles of Programming