0

The input is always passed as an string, regardless if i enter an int o str, but i'm testing if the variable is actually an integer.. if not, it raises an exception that calls the function recursively...

the logic works...

but the results returns as 'None' if I start the function with an string as a variable passed to it

I have tried all types of combinations...

The function must be executed like 'print(get_number())'

I can't print the result inside the function, because it is part of the problem specification

I have concluded that the problem is in the recursive function... but i cant figure it out

def get_number():
    val1 = input('Enter a number: ')
    try:
        val1 = int(val1)
        while val1 < 1 or val1 > 10:
            val1 = input('Enter a number: ')
            val1 = int(val1)

        str_to_print = '{:.1f}'.format(val1)
        return str_to_print

    except ValueError:
        get_number()


print(get_number())

These are the expected results:

and when done in that order the logic do run, but just that the resuls is 'None'

enter image description here

Weichafe
  • 97
  • 1
  • 10

1 Answers1

3

You're missing a return in the except block:

    except ValueError:
        return get_number()
mackorone
  • 1,056
  • 6
  • 15