From 113b92cbfc58e4ce6a5e6e7eabbacec67810595c Mon Sep 17 00:00:00 2001 From: biologyguy Date: Tue, 2 Aug 2016 18:06:15 -0400 Subject: [PATCH] Consider revising 'Strings to Numbers' exercise in episode 03 Make it explicit that trying to recast types that don't make sense will throw an exception, with a demonstration. Then include another nonsensical conversion for the actual exercise. --- _episodes/03-types-conversion.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/_episodes/03-types-conversion.md b/_episodes/03-types-conversion.md index a37874e83..d46f865a1 100644 --- a/_episodes/03-types-conversion.md +++ b/_episodes/03-types-conversion.md @@ -246,7 +246,7 @@ first is 2 and second is 5 > ## Strings to Numbers > -> `float` will convert a string to a floating point number, +> Where reasonable, `float` will convert a string to a floating point number, > and `int` will convert a floating point number to an integer: > > ~~~ @@ -261,7 +261,24 @@ first is 2 and second is 5 > ~~~ > {: .output} > -> Given that, +> If the conversion doesn't make sense, however, an error message will occur +> +> ~~~ +> print("string to float:", float("Hello world!")) +> ~~~ +> {: .python} +> +> ~~~ +> --------------------------------------------------------------------------- +> ValueError Traceback (most recent call last) +> in () +> ----> 1 print("string to float:", float("Hello world!")) +> +> ValueError: could not convert string to float: 'Hello world!' +> ~~~ +> {: .error} +> +> Given this information, > what do you expect this program to do? > What does it actually do? > Why do you think it does that?