A basic python question (exceptions) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

A basic python question (exceptions)

Hello all, I am trying to add an error so that no one will use a number for my input. How do I complete this program? Thank You!!! name =input("What is your name?") print("Hello", name) except IOError: print("An error has occurred")

30th Jan 2019, 9:41 PM
Markie Alicia
Markie Alicia - avatar
4 Answers
+ 5
Except keyword means you have a try keyword. So, write : try: name = input() print("Hello", name) except IOError: print("Error")
30th Jan 2019, 10:11 PM
Théophile
Théophile - avatar
+ 1
just do name = str(input(“What is your name?”))
30th Jan 2019, 10:10 PM
Zanir
Zanir - avatar
+ 1
Make sure to catch the right exception. An input will probably never raise an IOError (which means that a file can't be read or the disk is full) and was replaced by OSError in some python 3.x version. Also, watch your indentation. Line 3 must not be indented whereas line 4 has to be indented
31st Jan 2019, 3:17 AM
Anna
Anna - avatar
0
Try it with try statment. try: some code except IOError: other code
30th Jan 2019, 11:53 PM
Andrej Zrnic
Andrej Zrnic - avatar