Integer Operator in If Statements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Integer Operator in If Statements (/thread-2193.html) Pages:
1
2
|
Integer Operator in If Statements - warmth - Feb-25-2017 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? RE: Integer Operator in If Statements - Larz60+ - Feb-25-2017 Not tested (for syntax), by I think the sybtax is correct if isinstance( y/a, ( int ) ) and isinstance(x/a, (int)): RE: Integer Operator in If Statements - Skaperen - Feb-25-2017 the (int) can be just int when only one type is checked RE: Integer Operator in If Statements - buran - Feb-25-2017 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. RE: Integer Operator in If Statements - sparkz_alot - Feb-25-2017 Or you can use the 'fractions module'. >>> import fractions >>> fractions.Fraction(2,4) Fraction(1, 2) >>> fractions.Fraction(3,6) Fraction(1, 2) >>> RE: Integer Operator in If Statements - ichabod801 - Feb-25-2017 (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. RE: Integer Operator in If Statements - wavic - Feb-25-2017 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 RE: Integer Operator in If Statements - warmth - Feb-25-2017 (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.) 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 // RE: Integer Operator in If Statements - wavic - Feb-25-2017 What if: >>> x=int(input("Enter 1st value ")) Enter 1st value red
RE: Integer Operator in If Statements - buran - Feb-25-2017 (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. |