+ 1

Python n00b-please help! - if-else script

hi all, I can't figure out how to specify the user input I'm asking for to be type integer so I can run the rest of the if/ else piece: i = input("please provide a number that you like") input=type("int") if i <=5: print(i) else: print("whoa thats greater than 5!") print("Let me make that smaller for you:") i= i - 3 print(i) print("There,now that's better , take it easy fella!")

8th Oct 2017, 10:57 PM
Pete Smiddy
Pete Smiddy - avatar
7 Respostas
+ 3
Try this: i = int(input("please provide a number that you like")) if i <= 5: print(i) else: print("whoa thats greater than 5!") print("Let me make that smaller for you:") i= i - 3 print(i) print("There,now that's better , take it easy fella!")
8th Oct 2017, 11:46 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 2
if you want to specify the input type as an integer, just wrap it as an int i = int(input("Please provide a number that you like"))
9th Oct 2017, 12:23 AM
LordHill
LordHill - avatar
+ 1
I don't know Python's syntax, so I speak abstract i = input if is int(i) == false: raise TypeError: Given value must be an instance of integer, type(i) given.
8th Oct 2017, 11:23 PM
Freezemage
Freezemage - avatar
+ 1
Then use 'try-except' block to check if any of exceptions is raised. You can lesrn more in SL course
8th Oct 2017, 11:27 PM
Freezemage
Freezemage - avatar
0
Raise an exception.
8th Oct 2017, 11:07 PM
Freezemage
Freezemage - avatar
0
Thank you very much for the reply. How exactly would I do that?
8th Oct 2017, 11:17 PM
Pete Smiddy
Pete Smiddy - avatar
0
Thanks guys!
9th Oct 2017, 1:09 AM
Pete Smiddy
Pete Smiddy - avatar