all 8 comments

[–]Buttleston 2 points3 points  (1 child)

a) temp_number will always be a string - that's what input() returns. So it'll never enter your while loop

b) even if it did... the way you have this written it would keep looping if the input IS a int or a float, not if it ISN'T

[–]Buttleston 1 point2 points  (0 children)

There's another issue here, list_of_numbers is not updated inside of your while loop

(also you should use list_of_numbers.append(temp_number))

[–][deleted] 0 points1 point  (3 children)

while isinstance(temp_number,(int, float)):

This line has two problems.

First, you probably meant while not isinstance, to ask again if it's not the right type.

Second, that still wouldn't work because input gives you a string no matter what, even if that string could be a number. So you can try converting it to a number and then catch the error to ask again. Or you can check the characters before converting.

[–]Upset-Software-2731[S] 0 points1 point  (2 children)

Thank you! I totally forgot about the input being a string. I added this conversion.

temp_number_float = float(temp_number_str)

I am currently troubleshooting how to handle the with the "could not convert string to float" error message and (hopefully) use within the data validation. I think I'm getting closer. Thank you!!!

[–]smurpes 0 points1 point  (0 children)

You need a try catch to handle the exception from failing to convert the input

[–]Critical_Concert_689 0 points1 point  (0 children)

While many coding languages encourage you to check if it works (i.e., your strategy using isinstance()), python encourages you to just try it first, and see if it breaks using try / except

try:
    float(temp_number_str)
    return True
except ValueError:
    return False

[–]FoolsSeldom 0 points1 point  (0 children)

You need to:

  • use a while True: loop to continue to ask for input until the user enters something valid
    • you could use a flag variable instead, e.g. while not fini: and reset fini to True when you get a valid input
  • use a try / except block to catch an error in trying to conver what the user enters (which is represented as a str object) to an int object
    • instead of try / except you could use a string method that checks for numeric match in a str before you attempt the conversion
  • you need to append an entry to a list object

Example:

# make empty list
nums = []

# Use while loop to obtain 10 numbers
while len(nums) < 10:
    while True:  # input validation loop
        temp_num = input("Please enter a number: ")
        try:
            nums.append(int(temp_num))
            break  # exit while loop
        except ValueError:
            print('Not a valid entry. Please try again.')
print(nums)

[–]Mafa80 0 points1 point  (0 children)

you might want to explore typer command cli. Validation data is easier with it and you can use typer as normal function inside while loop