-1

I am building a basic calculator using python and I have a feature that will detect if the text entered is a number or not. For that I have defined a function ie

def check_num(num):
    while not num.isnumeric():
        print("Invalid number! Please enter a valid number.") 
        num = input()

When I take an input of the number it is taking just the first input.

num1 = input()
check_num(num1)

How do I make it take the correct input that is the last input?

Here is an image of the code running in command prompt:

screenshot

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

0

You'll want to change your function definition, so that the function returns num:

def check_num(num):
    while not num.isnumeric():
        print("Invalid number! Please enter a valid number.") 
        num = input()
    return num

and then call it like this:

num1 = check_num(input())
Paul M.
  • 10,481
  • 2
  • 9
  • 15
0

You could make a recursive function that asks for the number instead of simply checking it:

def ask_num():
    num = input()
    if num.is_numeric():
        return num
    print("Invalid number! Please enter a valid number.") 
    return ask_num()
Seon
  • 3,332
  • 8
  • 27