Python Forum
Integer Operator in If Statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Integer Operator in If Statements
#11
(Feb-25-2017, 04:10 PM)warmth Wrote: 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.

abs() is built-in function in python, no import is needed to use it. And if you still cant use it, you can try things like

if x < 0:
    x = -x
Reply
#12
Thanks to everyone! I guess im a newbie with coding, this is the intro to computer science course in our university, and I am really liking it so far!

To wavic : He told us to leave it like that, resulting in an error if a non float or int value is entered, and if a two zeros are entered for both values, it should result in a division by 0 error.

To Buran: Totally agree with you, I think I misworded that! I meant to say, is division the only operation which results in a float, and im guessing its like that because of the '//' operation, which will always result in an int.

To Zivoni: yea we still cant use it, he said no functions in general, but lol thanks now ill use that whenever I need an abs. And that if statement makes things so much easier!
Reply
#13
i presume ZeroDivisionError can be caught like any other exception

division is the only arithmetic operation (what i think you mean) of integers that gives a float.  even the % (given ints) gives an int.  but, you can get floats by some other operations like conversions from other types.  do you need floats?  do you need non-floats?

to find out about abs() ... https://docs.python.org/3/library/functions.html

to find lots of good stuff: ... https://www.python.org/doc/
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
(Feb-25-2017, 11:51 PM)warmth Wrote: I meant to say, is division the only operation which results in a float, and im guessing its like that because of the '//' operation, which will always result in an int.

I think you don't understand fully the difference between classic (python2.x), true (python 3.x and also the common math division) and floor division (as described in PEP 238). The type of the result will depend on type of dividend and divisor and which python version you use:

Python 2.x

Python3.x:
Reply


Forum Jump:

User Panel Messages

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