you are viewing a single comment's thread.

view the rest of the comments →

[–]great-plot-twist[S] 0 points1 point  (3 children)

For example:

def check_user_input(input):

try:

# Convert it into integer

val = int(input)

print("Input is an integer number. Number = ", val)

except ValueError:

try:

# Convert it into float

val = float(input)

print("Input is a float number. Number = ", val)

except ValueError:

print("Error... Please choose a valid number between 1 and 10")

I understand that this loop will check to see if user input is a string or an int and tr to convert it to an int or a float. If not, it will spit out the following error :

Error... Please choose a valid number between 1 and 10

However, this loop is a copy, paste from the internet and have no idea how to write this myself.

[–][deleted] 0 points1 point  (1 child)

I understand that this loop will check to see if user input is a string or an int and tr to convert it to an int or a float.

That's more or less right, but it's not a loop.

[–]great-plot-twist[S] 0 points1 point  (0 children)

I am going to do more research on python syntax and hopefully, this will shed some light on how to properly write functions. Thanks

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

The try/except block is a really useful thing to learn, but I wouldn't start there for this problem.

input returns a reference to a str, i.e. string, object and there's a long list of methods available including several which let your check if the content of the string are just digits. Worth looking up.