Python Forum
Integer Operator in If Statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Integer Operator in If Statements
#1
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?
Reply
#2
Not tested (for syntax), by I think the sybtax is correct
if isinstance( y/a, ( int ) ) and isinstance(x/a, (int)):
Reply
#3
the (int) can be just int when only one type is checked
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply
#5
Or you can use the 'fractions module'.

>>> import fractions
>>> fractions.Fraction(2,4)
Fraction(1, 2)
>>> fractions.Fraction(3,6)
Fraction(1, 2)
>>>
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
(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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(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 //
Reply
#9
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' 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020