Skip to content

Commit 2424473

Browse files
committed
Move comments to docstrings
1 parent 83894f8 commit 2424473

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed
Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
# number_input_examples.py
1+
"""Utility functions for prompting users for numeric input.
22
3-
# This file accompanies the Real Python article
4-
# "How To Read User Input As Integers". It contains
5-
# examples of functions that filter user input for
6-
# valid integers and floats.
3+
This file accompanies the Real Python article How To Read User Input As
4+
Integers. It contains examples of functions that filter user input for valid
5+
integers and floats.
6+
"""
77

88

99
def get_integer(prompt: str, error_message: str = None) -> int:
10-
# Prompts the user for an integer value. If the input is invalid,
11-
# prints error_message (if specified) and then repeats the prompt.
10+
"""Prompts the user for an integer value.
11+
12+
If the input is invalid, prints error_message (if specified) and then
13+
repeats the prompt.
14+
"""
1215
while True:
1316
try:
1417
return int(input(prompt))
@@ -18,8 +21,11 @@ def get_integer(prompt: str, error_message: str = None) -> int:
1821

1922

2023
def get_float(prompt: str, error_message: str = None) -> float:
21-
# Prompts the user for a float value. If the input is invalid,
22-
# prints error_message (if specified) and then repeats the prompt.
24+
"""Prompts the user for a float value.
25+
26+
If the input is invalid, prints error_message (if specified) and then
27+
repeats the prompt.
28+
"""
2329
while True:
2430
try:
2531
return float(input(prompt))
@@ -31,41 +37,42 @@ def get_float(prompt: str, error_message: str = None) -> float:
3137
def get_integer_with_default(
3238
prompt: str, default_value: int, error_message: str = None
3339
) -> int:
34-
# Prompts the user for an integer. If the input is an empty string,
35-
# returns default_value. Otherwise, if the input is not a valid integer,
36-
# prints error_message (if specified) and then repeats the prompt.
40+
"""Prompts the user for an integer, and falls back to a default value.
41+
42+
If the input is an empty string, then the function returns default_value.
43+
Otherwise, if the input is not a valid integer, prints error_message (if
44+
specified) and then repeats the prompt.
45+
"""
3746
while True:
3847
input_string = input(prompt)
39-
if input_string == "":
48+
if not input_string:
4049
return default_value
4150
try:
42-
result = int(input_string)
43-
return result
51+
return int(input_string)
4452
except ValueError:
4553
if error_message:
4654
print(error_message)
4755

4856

49-
# Run this file as a main module
50-
# (ie python number_input_examples.py)
51-
# to perform simple interactive testing of the functions.
52-
5357
if __name__ == "__main__":
54-
print(get_integer("Testing get_integer(): "))
58+
# Test functions when running as a script.
59+
print(get_integer("Test get_integer(): "))
5560
print(
5661
get_integer(
57-
"Testing get_integer() with an error message: ", "Invalid integer!"
62+
"Test get_integer() with an error message: ",
63+
error_message="Invalid integer!",
5864
)
5965
)
6066
print(
6167
get_float(
62-
"Testing get_float() with an error message: ", "Invalid float!"
68+
"Test get_float() with an error message: ",
69+
error_message="Invalid float!",
6370
)
6471
)
6572
print(
6673
get_integer_with_default(
67-
"Testing get_integer_with_default(): ",
68-
99,
69-
"That's not a valid integer!",
74+
"Test get_integer_with_default(): ",
75+
default_value=99,
76+
error_message="That's not a valid integer!",
7077
)
7178
)

0 commit comments

Comments
 (0)