Getting Input
Let’s ask the User¶
We do not always want to adapt our code when we try to run a different scenario. Instead we could let the user input the desired starting values.
To do so, we can use the built-in input(…)
-function.
It takes one argument, the so-called prompt, which is the text that asks the user to type in something.
Lets start by modifying how we set up the initial population:
Once the program reaches that line, it will stop and wait for the user to type something in and press the Enter-key.
If we try that out now, our prorgam will eventually run into some errors.
The reason is, that the input(…)
-function will hand over the what the user typed in as a string even though we want to have an integer for the starting population.
To remedy this issue we will have to use the type casting that we mentioned earlier.
Also, let’s introduce a new variable user_input
to hold the raw input as text, to make it clear what is going on step-by-step.
Our fixed code looks like this:
user_input = input("Please input the size of the starting population: ")
starting_population = int(user_input)
We now have a working code again and it has become more flexible through our change.
But sometimes, users do not understand correctly what is asked of them or may not know all of the rules their input has to satisfy.
In our case, someone might get the idea to input a negative number or 0
.
We can use a condition to check for that.
user_input = input("Please input the size of the starting population: ")
input_as_int = int(user_input) # A new variable, since we do not know if this is a valid starting population yet
if input_as_int >= 0: # That is valid
starting_population = input_as_int
else:
# Try again?
Can you repeat that, please?¶
The try again part is tricky. How often should we try again? Naturally, we would assume that we repeat the process until we have a valid input. To repeat a section of code, we can use a loop.
Loops in Python
In Python, loops come in two flavors:
- The
for
-loop repeats a set amount of times - The
while
-loop repeats as long as a condition is met.
For our use-case we will have to repeat the whole input-check validity-try again cycle until we have a valid input.
Also we will add the input_is_valid
variable to keep track of the current decisin regarding the input.
input_is_valid = False # We have no input yet, so it can't be valid
while not input_is_valid:
user_input = input("Please input the size of the starting population: ")
input_as_int = int(user_input)
input_was_valid = input_as_int >= 0 # Re-evaluate the validity
if not input_was_valid:
print("Sorry,", input_as_int, "is not a valid input, please try again")
# We are now outside the loop (no longer indented)
# The input_as_int should be valid now
starting_population = input_as_int
Now the user will repeatedly be asked to try again until they input a number larger than 0.
What have you learned so far?
This would be a good time to check your learning progress with some exercises!
Key Points
- The
input(…)
-function can be used to query the user for information - Remember to type cast the given input accordingly.
- Loops can be used to repeat sections of code
- A
while
-loop repeats as long as a condition is met.