160 likes | 335 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 import random defplay_rock_paper(): mapping2 = ["paper","scissors","rock"] again = "y" while again == "y": computer_num = random.randint(0,2) person2_choice = input('Choose "r" "p" or "s": ') person2_num = find_mapping(person2_choice) print("Computer chose " + mapping2[computer_num] +".") print("You chose " + mapping2[person2_num] + ".") winner = who_wins(mapping2[computer_num],mapping2[person2_num]) if winner == "person2": print("You win.") elif winner == "tie": print("We are tied.") else: print("Computer wins.") again = input('Do you want to play again? Answer "y" or "n": ') play_rock_paper() COMPSCI 101 - Principles of Programming
Helper Function • “who_wins” helper function from Slide 5 in Lecture 21 defwho_wins(person1_choice,person2_choice): if (person1_choice == "rock" and person2_choice == "scissors") or (person1_choice == "scissors" and person2_choice == "paper") or (person1_choice == "paper" and person2_choice == "rock"): return "person1" if person1_choice == person2_choice: return "tie" return "person2” deffind_mapping(person_choice): mapping = ["p","s","r"] for i in range(0,3): if person_choice == mapping[i]: return i 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 deftest_sudoku(board): for i in range(0,9): col = board[i:len(board):9] if not test(col): return False row = board[i*9:(i*9)+9] if not test(row): return False for column in range(0,3): for row in range(0,3): start_square = column*3+row*9*3 square = make_square(start_square, board) if not test(square): return False return True COMPSCI 101 - Principles of Programming
Helper Functions deftest(my_list): for i in range(0,9): if my_list[i] != 0 and my_list[i] in my_list[i+1:]: return False return True defmake_square(start,board): square = [] for row in range(0,3): for col in range(0,3): square += [board[start + (row * 9) + col]] return square 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