[Tutor] Dividing a float derived from a string

Danny Yoo dyoo at hashcollision.org
Thu Nov 20 23:36:48 CET 2014


> I have been posed with the following challenge:
>
> "Create a script that will ask for a number. Check if their input is a
> legitimate number. If it is, multiply it by 12 and print out the result."
>
> I was able to do this with the following code:
>
> input = raw_input("Insert a number: ")
> if input.isdigit():
>     print int(input) * 12
> else:
>     print False
>
> However, a colleague of mine pointed out that a decimal will return as
> False.


Hi Stephanie,


Let me try to reproduce the problem you're reporting.  I'll use the
interactive interpreter to help me.

##############################################
>>> def f():
...     n = raw_input("Insert a number:")
...     if n.isdigit():
...         print int(n) * 12
...     else:
...         print False
...
>>> f()
Insert a number:10
120
###############################################

This is just slightly different than the program you've given me.  I'm
replacing the variable name "input" with "n" to avoid potential
confusion, as there is a separate toplevel function called "input" in
the Python standard library.  I'm also defining it as a function
called f(), just to make it easy for me to call it multiple times.


>From the transcript above, I don't think I can see your problem yet.
It would be helpful to present a concrete example of a failure, to
make problem reproduction easier ... oh!  Ah, I see what you're saying
now, I think.

By "decimal", you probably don't just mean a number in base-10: you
may mean a number that has a _decimal point_.

##############################
>>> f()
Insert a number:3.14
False
##############################

Is this the problem that you're considering?


If so, you should be able to handle this additional case without too
much trouble.  You're learning how to use conditional statements, and
there's nothing that prevents you from expanding a branch that
considers one possible, to one that considers multiple possibilities:

####################################
if blah blah blah:
    do some thing
elif blah blah blah:
    do something else
else:
    do something even more different
####################################


You might check, additionally, to see if the string has a decimal
point, and then if it does, check that the left and right hand parts,
before and after the decimal, are all digits.


The find() method on strings can be helpful:

############################
>>> s = "3141.5926"
>>> s.find(".")
4
############################

If we know where the decimal is, we can start slicing up the string
into the left and right hand sides, using the string slicing operator:

#############################
>>> s[0:4]
'3141'
>>> s[4:]
'.5926'
##############################


So you can probably adjust your program to check for this possibility.


But to get this right in full generality is surprisingly a _lot_ more
tedious than you might initially expect.  :P  You should really check
with your instructor.  When the problem says "number", what does the
problem mean?

This is not as dumb a question as it sounds at first.  As an example
of what can make this complicated: is "3/4" considered a number,
according to the problem statement?  What about scientific notation?
If you're really pedantic (or have a sense of humor), you might even
ask yourself: how about roman numerals?

So you really should touch base with your instructor.  That will
probably help in clarifying what does appear to be a under-defined
term in the problem statement.


More information about the Tutor mailing list