1 / 16

COMPSCI 101 Principles of Programming

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.

eldon
Download Presentation

COMPSCI 101 Principles of Programming

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. COMPSCI 101Principles of Programming Lecture 24 Random Numbers, User Input and While Loops Dr. Patricia J. Riddle

  2. 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

  3. Review random.randint(1,8) • Returns a random integer between 1 and 8 (inclusive) • Must import the random module COMPSCI 101 - Principles of Programming

  4. 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

  5. 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

  6. 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

  7. 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

  8. Answer COMPSCI 101 - Principles of Programming

  9. Helper Function • “who_wins” helper function from Slide 5 in Lecture 21 COMPSCI 101 - Principles of Programming

  10. More Nested Loop Practice COMPSCI 101 - Principles of Programming

  11. 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

  12. Suduko Example COMPSCI 101 - Principles of Programming

  13. Answer COMPSCI 101 - Principles of Programming

  14. Helper Functions COMPSCI 101 - Principles of Programming

  15. Summary • While loops • User input • Random number generation COMPSCI 101 - Principles of Programming

  16. Next Tuesday • Dictionaries COMPSCI 101 - Principles of Programming

More Related