-1

Hi fairly new to python and I cant get my head round why this isn't working.

I want to have an object the user to input something as a string, if the string can also be a float, I want the object type to be a float.

def Representsfloat(s):
    try:
        float(s)
        return True
    except ValueError:
        return False


item_finder = input("Enter an Item to find: ")


def account_finder():
    if Representsfloat(item_finder) == True:
        item_finder = float(item_finder)
    else:
        pass

Thanks

AF9
  • 1
  • What exactly is not working? – mkrieger1 Aug 16 '20 at 09:37
  • You have a scope-problem. the `item_finder` inside `account_finder()` is totally different from the `item_finder` in global scope. if you modify a same-named variable inside a function it does not change the outer scope variable. common way to solve that is to assign a return of the function back to the outer scope variable: `item_finder = account_finder(item_finder)` && `return float(floated_item_finder)` inside the function – Patrick Artner Aug 16 '20 at 09:38

3 Answers3

1

First off, you are defining a function but never calling it.

Secondly, your code can't call a function which is not yet defined at that point in the code; on the other hand, the function definition can't refer to a global variable which is not yet defined. But the solution to that is to not use a global variable, which is an improvement in its own right.

Third, instead of having a separate function to check if you can convert a value to a float, just convert it.


def account_finder(value):
    try:
        return float(value)
    except ValueError:
        return value

for item_finder in ['1.234', '', 'nope', '12', '1.23e456']:
    print('%s -> %r' % (item_finder, account_finder(item_finder)))

Demo: https://ideone.com/fhtLAr

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

For values that may not exist, in Python we use None instead of boolean flags.

def try_float(s):
    try:
        return float(s)
    except ValueError:
        return None

In Python 3.8, we have :=, assignment, "walrus" operator, which is both an assignment and expression at the same time, meaning that most succint and idiomatic code would be

if f := try_float('2.56'):
    print(f)

Pre 3.8, you'd have to retrieve the value before the if statement:

f = try_float('2.56')
if f:
    print(f)
Yamirui
  • 169
  • 6
  • That’s nice, but it doesn’t explain what is the problem with the original code. – mkrieger1 Aug 16 '20 at 09:38
  • @mkrieger1 It parses and discards the value just to return whether it succeeded, there's nothing wrong with it when you just want to know if it is a float or not, but when you want to know if it is a float and both know the value, it repeats itself, which is both bad for performance and readability. Also the function I wrote can be used to check if it is a float and then discard the value anyway, the difference is that the user decides whether they need value or not. – Yamirui Aug 16 '20 at 09:42
  • Yes that’s true and I agree with you. But with "what is the problem" I meant why the code in the question didn’t work at all, not how it could be made more readable. – mkrieger1 Aug 16 '20 at 09:45
  • The problem is you need to strip the quotes that is why it is not working. Do like this account_finder(item_finder.strip("'")) – Dunski Aug 16 '20 at 09:47
  • @mkrieger1 I only aimed to answer the direct question as is because it was obvious that it will be marked as dupe and redirected to answers related to scoping, I'm just adding on top of that with something I deem to be useful knowledge which they won't see after being told to read about scoping, solving two problems at once. – Yamirui Aug 16 '20 at 09:48
-1

One function should do the trick

def Representsfloat(s):
    try:
        return float(s)
    except ValueError:
        return s

while True:
    item_finder = Representsfloat(input("Enter an Item to find: "))

I personally would do this:

while True:
    item_finder = input("Enter an Item to find: ")
    try: item_finder = float(item_finder)
    except ValueError: pass
    print(type(item_finder), item_finder)
leopardxpreload
  • 767
  • 5
  • 17