Python 5.2 : Exercise : ask number
The goal of this exercise is to practise functions, parameters and string formatting at the same time. We are going to build a function called ask_number that asks the user to enter a magic number between a minimum and a maximum given as parameters, and that returns the value as an integer. Try writing it on your own first, then come back to compare with the correction.
Reading and converting the input
Inside the function we call input with a prompt that displays the allowed range. The prompt is built with the classic %s placeholders we saw with string concatenation, replaced by the minimum and the maximum. The value returned by input is always a string, so we convert it explicitly with int before returning it. That way the caller always receives a real number.
def ask_number(min_number, max_number):
str_answer = input("What is the magic number between %s and %s? " % (min_number, max_number))
int_answer = int(str_answer)
return int_answer
ask_number(min_number, max_number)
When we run the program it asks for the magic number, we type a value such as 4, press Enter and the function returns it as an integer. The reason we convert the answer to an integer right inside the function is that it makes the rest of the program much simpler: every time we compare the number to a min or max, we are already working with the correct type and we do not have to think about it again. In the next video we will use this helper inside conditions to build the rest of the game.
Summary
This exercise teaches you to build a function that prompts the user to enter a number (called the "magic number") within a specified range defined by minimum and maximum parameters. The function uses string formatting to display a friendly prompt, accepts user input, converts it to an integer using int(), and returns the result. This pattern strengthens your understanding of type conversion and prepares you for implementing conditional logic in subsequent lessons.
Key points
- Define a function that accepts min and max number parameters
- Use string formatting to display a prompt showing the valid range to the user
- Call the input() function to capture the user's input as a string
- Convert the string input to an integer using the int() function
- Return the converted integer value from the function
- This pattern simplifies conditional checking in future exercises by handling input conversion centrally
FAQ
What is the 'magic number' in this exercise?
The magic number is the value that the user enters when prompted. It represents a number that should fall within the specified range defined by the min and max parameters passed to the function.
Why must we convert the user input to an integer?
User input from the input() function is received as a string by default. Converting it to an integer allows you to perform mathematical comparisons and arithmetic operations needed for conditional logic in subsequent exercises.
What are the function parameters and why are they needed?
The function takes min and max parameters that define the valid range for the number the user should enter. These parameters make the function reusable for different number ranges.