Python Forum

Full Version: Integer Operator in If Statements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
x=int(input("Enter 1st"))
y=int(input("Enter 2nd"))
a=0
if x>y and x<0:
        a=-x
if x>y and x>0:
        a=x
if y>x and y<0:
        a=-y
if y>x and y>0:
        a=y
if x==y:
    a=x
gcd=-999999999
while gcd!=a:        
    if y/a==int and x/a==int:
        print(a)
        gcd=a
    else:
        a=a-1
        print("wow")
    print("why doesnt this work")
Soooo, this is my code, its attempting to find a greatest common denominator from two input values, and Ive pinpointed the error to be the if statement saying
if y/a==int and x/a==int
.
Can anyone explain why this does not work?
Not tested (for syntax), by I think the sybtax is correct
if isinstance( y/a, ( int ) ) and isinstance(x/a, (int)):
the (int) can be just int when only one type is checked
In python3 y/a and x/a will always be float. (And in python2 - always int, given that x,y and a are always int.)
So once the problem with the syntax is resolved the code will still not work as expected. I think you want to check % (modulo) operator.
Or you can use the 'fractions module'.

>>> import fractions
>>> fractions.Fraction(2,4)
Fraction(1, 2)
>>> fractions.Fraction(3,6)
Fraction(1, 2)
>>>
(Feb-25-2017, 02:02 PM)sparkz_alot Wrote: [ -> ]Or you can use the 'fractions module'.

I don't think his teacher would accept that. Wink
It will be good if you wrap input statements into try/except block. One can type 'a' and 'b' for x and y. This will rise an exception and program will exit with an errog message
(Feb-25-2017, 07:30 AM)buran Wrote: [ -> ]In python3 y/a and x/a will always be float. (And in python2 - always int, given that x,y and a are always int.)
So once the problem with the syntax is resolved the code will still not work as expected. I think you want to check % (modulo) operator.


Thanks Buran I tried this and it worked, also, ichabod your right! No import functions! Otherwise I could just import math and use the abs value function instead of all those slimy if statements.

Here is a final copy of my code!

x=int(input("Enter 1st value "))
y=int(input("Enter 2nd value "))
a=0
if x>y and x<0:
        a=-x
if x>y and x>0:
        a=x
if y>x and y<0:
        a=-y
if y>x and y>0:
        a=y
if x==y:
    a=x
gcd=-999999999
while gcd!=a:        
    if y%a==0 and x%a==0:
        print(a)
        gcd=a
    else:
        a=a-1
    print("By golly shes working!")    
But this has left me wondering, what other operators result in floating point numbers?? or is it just division since there is another operator we could use for integer division, which is just two slashes instead of one //
What if:

>>> x=int(input("Enter 1st value "))                                                                      
Enter 1st value red                                                                                       
Error:
Traceback (most recent call last):                                                                           File "<stdin>", line 1, in <module>                                                                      ValueError: invalid literal for int() with base 10: 'red'                                                  invalid literal for int() with base 10: 'red' 
(Feb-25-2017, 04:10 PM)warmth Wrote: [ -> ]since there is another operator we could use for integer division, which is just two slashes instead of one //
// is called "floor division". 5//2 =2 and 4//2 =2, so I don't see how this can be used in this case.
Pages: 1 2