0

How do you put error text in a loop?

My program is somewhat like this: while True: print("^^^options^^^") print() print("select your number") print("bin [1]") print("ban [2]") print("bun [3]") user_input = input("==> ") (#the rest is output of 1 , 2, and 3) if i were put a number other than 1, 2 , 3, you guys know that the script will loop to the 1st line. Is there any way to put an error text like" You enter a wrong number. Please check your input" before it back to the 1st line? Thank you. Sorry for asking a noob question!!!

9th Feb 2017, 12:58 PM
Nightmareside96
Nightmareside96 - avatar
7 Answers
+ 3
raise(#your error): Only works for existing errors in Python OR if #condition: #your error #your code: You can create your own error Also we were beginners once too
9th Feb 2017, 1:09 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 3
how about after the last line you add if (user_input > 3): print("error! wrong number! try again.")
9th Feb 2017, 1:47 PM
Mario L.
Mario L. - avatar
+ 3
lst = ['1', '2', '3'] if (user_input not in lst): // error
9th Feb 2017, 2:17 PM
Mario L.
Mario L. - avatar
+ 2
Ypu can use raise everywhere
22nd Feb 2017, 7:54 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 1
@Ali Emir Kizir: i still confused. Where should i put this 'raise' thing btw? Tried once and don't get what it mean :p Can you please write down a simple script that contain 'raise'?
9th Feb 2017, 1:40 PM
Nightmareside96
Nightmareside96 - avatar
+ 1
@Mario Laurisch JUST TRIED IT ON QPYTHON 3 AND IT WORK LIKE MAGIC!!! THANK YOU VERY MUCH Now I can continue my project! Case closed
9th Feb 2017, 2:28 PM
Nightmareside96
Nightmareside96 - avatar
0
@Mario Laurisch Hm, i think it was true for integer type input. However, what if the user somewhat type a string? Thanks BTW, I can see some light :p
9th Feb 2017, 1:56 PM
Nightmareside96
Nightmareside96 - avatar