0

`Hi, I want to write a code to get inputs for both integer and float numbers but only numbers. If float number is given it accepts dot but not any other nonalphanumeric characters.

I wrote this code but it gives "Please enter the Frequency value:" error for float numbers, too.`

def signal_selection(signal_type):
   if signal_type == "1":  # Sine wave
        # Frekans değerini input olarak alır
        frequency = input("Please enter the Frequency value:")  # Hz
        while (frequency.isnumeric() != True):
            if frequency.isalpha():
                frequency = input("Please enter a valid number:")
            elif frequency.isnumeric() != True:
                frequency = input("Please enter a valid number:")
            elif frequency.isdigit() != True:
                frequency = input("Please enter a valid number:")
        frequency = float(frequency)
        print("Frequency:", frequency, "\n")
oznurc
  • 33
  • 4
  • 2
    `isnumeric` only returns True for integers; see this post: https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-represents-a-number-float-or-int – Swifty Jul 20 '23 at 15:14
  • 2
    @Swifty Not exactly. `'²'.isnumeric()` gives back `True`, but `int('²')` does not work. – Matthias Jul 20 '23 at 15:59
  • Indeed, I realized I was very approximative; I should have said it doesn't acknowledge decimals. – Swifty Jul 21 '23 at 07:23

3 Answers3

1

An easier way of doing it is by using the try-except construct:

def signal_selection(signal_type):
   if signal_type == "1":  # Sine wave
        # Frekans değerini input olarak alır
        frequency = input("Please enter the Frequency value:")  # Hz
        while True:
            try:
                frequency = float(frequency)
                break
            except ValueError:
                frequency = input("Please enter a valid number:")
        print("Frequency:", frequency, "\n")

i.e. trying to convert it to float and ask again if the conversion fails.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
0

As mentioned by @Swifty, the isnumeric check doesnt work for float-like strings. Therefore, frequency.isnumeric() != True becomes true on entering a float-like string and causes another input to take place.

An easier and alternate way of doing this is using error handling along with a while loop:

def signal_selection(signal_type):
    if signal_type == "1":  # Sine wave
        # Frekans değerini input olarak alır
        frequency = input("Please enter the Frequency value:")  # Hz
        valid_input = False
        while not valid_input:
            try:
                frequency = float(frequency)
            except ValueError:
                frequency = input("Please enter a valid number:")
            else:
                valid_input = True
        print("Frequency:", frequency, "\n")
Mridul
  • 127
  • 7
-1

What about using the following function to check the number:

def is_number(n):
    try:
        float(n)
    except ValueError:
        return False
    return True
Alireza Roshanzamir
  • 1,165
  • 6
  • 17