-4

I can't use if statements and loops in my assignment so I was wondering how I would rewrite this line:

  if (not float(gravity).is_integer()):

It's just checking to see whether or not gravity is a float or integer so it can pass more code.

javax
  • 7
  • 7
  • 1
    Can you use `try`/`except`? – roganjosh Sep 19 '18 at 22:24
  • Yes i think so. – javax Sep 19 '18 at 22:25
  • In that case, do that. `if` is a look-before-you-leap approach (can I do it before I try and do it?) whereas Python tends towards "asking forgivenesss" (let's try it and recover if I fail). Look up exception handling. – roganjosh Sep 19 '18 at 22:26
  • so i used try and except, but isn't except used for catching errors not used as an "if-else"? – javax Sep 19 '18 at 22:30
  • 1
    Quick question, why are you converting it to a float to test if it is an integer or float? – SShah Sep 19 '18 at 22:30
  • 1
    @javax re-read my comment. In some languages this might be taboo, but in Python a try/except can be used to control flow in Python – roganjosh Sep 19 '18 at 22:32
  • @SShah Honestly, the point of this function is to put two numbers into an input and see whether or not the Pythagorean theorem result will be an integer or float, and my alternative method was weird because square root of 100 is 10, but since its math.sqrt() its 10.0, which is a float. Therefore it always returns a float value so no matter what its going to return False to the user. This method just worked for some reason. – javax Sep 19 '18 at 22:33
  • it's not clear exactly what you're trying to do, but you can use a shortcut operator with a boolean result, without having to use an if statement, e.g. `gravity.is_integer() and do_stuff()` <-- this will only evaluate `do_stuff()` if the first part is true. – Tasos Papastylianou Sep 19 '18 at 22:35
  • My very wild guess is that `float(gravity).is_integer()` should _always_ return `False`. Therefore you should _always_ execute the code in the `if`-block. That is, just get rid of this `if` line altogether. – AGN Gazer Sep 19 '18 at 22:37
  • @javax All I am trying to understand is, if your output is always a float e.g. 10.0, then why are you converting the value to a float again, `float(gravity)`? – SShah Sep 19 '18 at 22:38
  • @SShah e.g. float("10.3") – Tasos Papastylianou Sep 19 '18 at 22:39
  • oh okay so its to convert a string i see very well, makes sense – SShah Sep 19 '18 at 22:40
  • 1
    I was just asking, because if your outputs varied between a float and an int, then you could have used `isinstance(gravity, int)` and `isinstance(gravity, float)`, but this confirms it you definitely cant use them – SShah Sep 19 '18 at 22:41
  • @SShah I don't know, I'm not the OP. I'm just guessing that it may be something like that; I agree, converting a float to a float is redundant, so presumably gravity is not necessarily a float to begin with. – Tasos Papastylianou Sep 19 '18 at 22:42
  • I just tried the following on my idle `float("10.0").is_integer()` outputs `True` and `float("10.20").is_integer()` which outputs `False`, so maybe you can try store the statement `float(gravity).is_integer()` into a `variable` e.g. `x = float(gravity).is_integer()` and then try `if (not x)` – SShah Sep 19 '18 at 22:44
  • But how did `gravity` become a string if it's the result of a Pythagorean triple calculation? – PM 2Ring Sep 19 '18 at 23:00

3 Answers3

1

You can use shortcut operators to achieve logical flow without an if statement. E.g.

float(gravity).is_integer() and do_stuff()

The second part will only execute if the first part is true.

Alternatively you can use

float(gravity).is_integer() or do_stuff()

where the second part will only execute if the first part is false.

UPDATE

I just read the comment about how the function is simply meant to evaluate if two sides yield an integer hypotenuse. So unless I misunderstood what you're after here, in that case, the whole point is that you don't need an if statement to decide whether you should then explicitly return True or False by yourself; you can simply return the output of the evaluation of is_integer() directly, since this will evaluate to either True or False anyway.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
1

My guess is that the assignment is trying to teach you a paradigm of "Ask forgiveness not permission".

In that case:

try:
    gravity = float(gravity)
    # Do floaty gravity stuff
except (TypeError, ValueError):
    gravity = some_default_value_you_can_handle_some_other_way
roganjosh
  • 12,594
  • 4
  • 29
  • 46
0

You could use assert in combination with a try block:

try:
    assert(not float(gravity).is_integer())
    print("evaluated to true")
except:
    print("evaluated to false")

Replace the print statements with the code you want to execute in case it evaluates to true or false.

Flu
  • 45
  • 1
  • 5
  • Awesome; an unrecoverable position :P – roganjosh Sep 19 '18 at 23:00
  • 1
    `assert` should not be used to check values like that. It's purpose is to check program logic. If AssertionError is raised that means the program logic is invalid and the program needs to be modified. – PM 2Ring Sep 19 '18 at 23:02
  • @PM 2Ring I agree, but here it's about a simple program where he just needs to replace the ```if```s. I doubt the assignment is that complex for the asserts to actually reveal some faulty logic. For the time being, this is a clear solution. Although I see what you mean and how this might not be such a good idea. Thanks for the feedback – Flu Sep 19 '18 at 23:15
  • 2
    Fair enough. I guess weird assignment specifications can justify weird code. ;) I just don't like people to see `assert` being used to validate data, since they may think that's what `assert` is for. – PM 2Ring Sep 19 '18 at 23:44