Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for optional args for parity with newest C library #17

Merged
merged 2 commits into from
May 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,11 @@ def write(self, x):
sys.stdout = flushfile(sys.stdout)

def get_char(prompt=None):
"""Read a line of text from standard input and return the equivalent char."""
"""
Read a line of text from standard input and return the equivalent char;
if text is not a single char, user is prompted to retry. If line can't
be read, return None.
"""
while True:
s = get_string(prompt)
if s is None:
@@ -34,7 +38,11 @@ def get_char(prompt=None):
print("Retry: ", end="")

def get_float(prompt=None):
"""Read a line of text from standard input and return the equivalent float."""
"""
Read a line of text from standard input and return the equivalent float
as precisely as possible; if text does not represent a double, user is
prompted to retry. If line can't be read, return None.
"""
while True:
s = get_string(prompt)
if s is None:
@@ -50,7 +58,11 @@ def get_float(prompt=None):
print("Retry: ", end="")

def get_int(prompt=None):
"""Read a line of text from standard input and return the equivalent int."""
"""
Read a line of text from standard input and return the equivalent int;
if text does not represent an int, user is prompted to retry. If line
can't be read, return None.
"""
while True:
s = get_string(prompt);
if s is None:
@@ -69,7 +81,11 @@ def get_int(prompt=None):

if sys.version_info.major != 3:
def get_long(prompt=None):
"""Read a line of text from standard input and return the equivalent long."""
"""
Read a line of text from standard input and return the equivalent long;
if text does not represent a long, user is prompted to retry. If line
can't be read, return None.
"""
while True:
s = get_string(prompt)
if s is None:
@@ -85,11 +101,18 @@ def get_long(prompt=None):
print("Retry: ", end="")

def get_string(prompt=None):
"""Read a line of text from standard input and return it as a string."""
"""
Read a line of text from standard input and return it as a string,
sans trailing line ending. Supports CR (\r), LF (\n), and CRLF (\r\n)
as line endings. If user inputs only a line ending, returns "", not None.
Returns None upon error or no input whatsoever (i.e., just EOF).
"""
try:
if prompt is not None:
print(prompt, end="")
s = sys.stdin.readline()
if not s:
return None
return re.sub(r"(?:\r|\r\n|\n)$", "", s)
except ValueError:
return None