+ 1
Python: How to give the user infinite attempts to provide the correct input type
Hello everyone, I'm trying to figure out how to give a user unlimited attempts to provide the right type of input. I'm new and not sure how to put all the pieces together, but this is what I've got so far: try: x = float(input("Give me a number: ")) if isinstance(x, float): print("Thanks dude!") except ValueError: print("That's not a number.") x = float(input("Please give me a number: ")) if isinstance(x, float): print("Thank you!") while x != float: x = float(input("Pretty please give me a number: ")) else: print("Thanks a lot!") I'd really appreciate some suggestions on this. Thanks everyone!
4 Réponses
+ 8
while True:
try:
x = float(input("Give me a number: "))
print("Thank you!")
break
except ValueError:
print("That's not a number.")
+ 5
Derrick however if try to run this on sololearn it would not works as espected, as sololearn code console is not interactive
+ 4
@Anna That works great! Way simpler. I was overthinking it, and I see now that the while statement should have come first.
Thanks a lot!
+ 2
Mind To Machine 💻🕆 thank you for that. Yes I'm using python and solo learn on my laptop for most all my learning.