"""Lab 1.""" # Boolean Operators def both_positive(x, y): """Returns True if both x and y are positive. >>> both_positive(-1, 1) False >>> both_positive(1, 1) True """ return x and y > 0 # You can replace this line! # If Statements def xk(c, d): if c == 4: return 6 elif d >= 4: return 6 + 7 + c else: return 25 def how_big(x): if x > 10: print('huge') elif x > 5: return 'big' elif x > 0: print('small') else: print("nothin'") def so_big(x): if x > 10: print('huge') if x > 5: return 'big' if x > 0: print('small') print("nothin'") # Guessing Games from random import randint LOWER = 1 UPPER = 10 def guess_random(): """Guess randomly and return the number of guesses.""" prompt_for_number(LOWER, UPPER) # asks the user to choose a number num_guesses, correct = 0, False while not correct: guess = randint(LOWER, UPPER) # randomly guess correct = is_correct(guess) # ask user if guess is correct num_guesses = num_guesses + 1 return num_guesses def guess_linear(): """Guess in increasing order and return the number of guesses.""" prompt_for_number(LOWER, UPPER) num_guesses = 1 guess = LOWER "*** YOUR CODE HERE ***" return num_guesses def guess_binary(): """Return the number of attempted guesses. Implement a faster search algorithm that asks the user whether a guess is less than or greater than the correct number. Hint: If you know the guess is greater than the correct number, then your algorithm doesn't need to try numbers that are greater than guess. """ prompt_for_number(LOWER, UPPER) num_guesses = 1 lower, upper = LOWER, UPPER guess = (lower + upper) // 2 "*** YOUR CODE HERE ***" return num_guesses # Receive user input. You do not need to understand the code below this line. def prompt_for_number(lower, upper): """Prompt the user for a number between lower and upper. Return None.""" is_valid_number = False while not is_valid_number: # You don't need to understand the following two lines. number = input('Pick an integer between {0} and {1} (inclusive) for me to guess: '.format(lower, upper)) number = int(number) if lower <= number <= upper: is_valid_number = True def is_correct(guess): """Ask the user if a guess is correct and return whether they respond y.""" return is_yes('Is {0} your number? [y/n] '.format(guess)) def is_too_high(guess): """Ask the user if a guess is too high and return whether they say yes.""" return is_yes('Is {0} too high? [y/n] '.format(guess)) def is_yes(prompt): """Ask the user a yes or no question and return whether they say yes.""" while True: # This while statement will loop until a "return" is reached. yes_no = input(prompt).strip() if yes_no == 'y': return True elif yes_no == 'n': return False print('Please type y or n and press return/enter')