Elmi
Elmi

Reputation: 6203

Simple if-comparison in Python fails with float

Sorry für this incredible stupid beginners question but I fail with a simple comparison of two numeric values in python. That's my relevant code:

lastResult=1.0
currResult=repr(model.evaluate(xTest, yTest)[1]) # now currResult contains 0.0

if (currResult>lastResult):
  print("\nBetter result " +str(currResult) + " > " + str(lastResult))

Here the comparison fails, although currResult is smaller than lastResult, the if-condition is executed and I get some output

Better result 0.0 > 1.0

Any idea what I'm doing wrong here?

Upvotes: 2

Views: 75

Answers (1)

GeeTransit
GeeTransit

Reputation: 1468

Simple little error. repr returns a string. Use int instead. :D

@BlackBear Looks like it should return a float.

lastResult = 1.0
currResult = float(model.evaluate(xTest, yTest)[1])

# note that this is 0.0 > 1.0 and shouldn't run
if currResult > lastResult:
    print(f'\nBetter result {currResult} > {lastResult}')

Upvotes: 3

Related Questions