input functioninput functioninput()will wait for user input and return as a stringinput("prompt")will print a prompt and then wait for user input return as a stringRemember that type casting fuctions like int() and float() will throw errors if a string argument has non-numeric characters
print functionsCheck the documentation for print() – what optional parameters can we change?
print sends data to end-user, can’t be used elsewhere.input reads data from end-user, can’t be from elsewhere.This function is useless and I hate it:
Write a basic number guessing game. Here’s some code to get you started:
How do we add input validation to this solution? (to ensure int() won’t throw an error)
import random
def guess(user_input, secret):
user_input = int(user_input)
if user_input == secret:
print("correct")
return True
if user_input > secret:
print("smaller")
return False
if user_input < secret:
print("larger")
return False
def play_game(secret):
user_input = input("Guess the number: ")
while not guess(user_input, secret):
user_input = input("Guess the number: ")
if __name__ == "__main__":
MIN = 1
MAX = 100
SECRET = random.randint(MIN, MAX)
play_game(SECRET)You have 10 minutes to complete the quiz
if __name__. Just write your function and what’s inside the functionHINTS:
def to define a function% to determine if a number is odd (the remainder of the division by two is not zero)