[Tutor] LCM
Alan Gauld
alan.gauld at btinternet.com
Thu Oct 21 10:14:39 CEST 2010
"Jacob Bender" <benderjacob44 at gmail.com> wrote
> decided to make a program that deals with them. Here's the code:
>
> thing = raw_input("What is the first number?\n\n")
> thing2 = raw_input("What is the second number?\n\n")
>
> int(thing)
> int(thing2)
This does nothing useful. It converts the strings to numbers
which are immediately thrown away because they are not
assigned to anything.
> x = 1
>
> thing3 = x/thing
> thing4 = thing2/x
This will likely fail because things are still strings.
But even if they were numbers itwould be clearer
in this case to not use x, just put the 1 directly in
the calculation.
> def calc():
> while True:
>
> if isinstance(thing3, int) == True:
> if isinstance(thing4, int)== True:
> print "The LCM is"
> print x
So any time thing3 and 4 are integers x will be 1
> raw_input()
> break
> else:
> x + 1
Again this does nothing useful because it adds one to x
then throws away the value
> calc()
This will cause an infinite loop (until Python runs
out of recursion space). I'm not even sure what
you think this will achieve?
> else:
> x + 1
> calc()
See above...
> The only problem is that when I tell it that thing3 = x/thing it
> gives an error. Here it is:
>
> Traceback (most recent call last):
> File "C:/Documents and Settings/Family/My Documents/LCM.py", line
> 10, in <module>
> thing3 = x/thing
> TypeError: unsupported operand type(s) for /: 'int' and 'str'
See above comments.
You need to retyhink your algorithm and pay attention to assigning
results to variables.
And once you have done that you may want to make x a parameter
of calc(), or at least declare it a global variable.
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list