why I'm getting is error ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why I'm getting is error ?

age = int(input('age: ')) print(age) output = IndentationError : unexpected indent

15th May 2020, 6:09 AM
Alapottra Chakma
Alapottra Chakma - avatar
12 Answers
+ 2
Did the error message include infoemation about line number? as I see it, the code only contains 2 lines, and (since it is raw text), it's not really clear where the unexpected indentation is. Is that the whole code? maybe the unexpected indentation was somewhere else Idk ...
15th May 2020, 6:29 AM
Ipang
+ 2
Maybe get the input before the try-statement and use try only for type conversion? Cause in try, it might not wait for input but directly jumps to except (or finally), and not reach the definition of X and Y. age = input() try: age = int(age) except: age = None print('invalid age')
15th May 2020, 6:46 AM
Lisa
Lisa - avatar
+ 2
try: age = int(input("age: ")) X= age name = str(input("name")) Y=name except ValueError: print("invaild value") finally: print (X, Y ) With the given code above, you should only get the NameError: 'X' is not defined if the try block fails when entering an age that cannot be converted to an int, as the X= age line will be jumped over to get to the except block and then finally will be always ran giving you that error.
15th May 2020, 6:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Hi, getting input within try-statement seems to be a problem (unlike I assumed), but in the except block, you should dummy code X and Y when conversion fails and you want to print them in finally
15th May 2020, 7:02 AM
Lisa
Lisa - avatar
0
try: age = int(input("age: ")) X= age name = str(input("name")) Y=name expect : ValueError: print("invaild value") finally: print (X, Y )
15th May 2020, 6:34 AM
Alapottra Chakma
Alapottra Chakma - avatar
0
now it's saying undefined X Y
15th May 2020, 6:34 AM
Alapottra Chakma
Alapottra Chakma - avatar
0
nameError: name 'X' is not defined
15th May 2020, 6:34 AM
Alapottra Chakma
Alapottra Chakma - avatar
0
I presume <X> and <Y> are only available within the `try` block, where they were defined. You are using them in `finally` block.
15th May 2020, 6:43 AM
Ipang
0
any way to fix this ? should i remove finally block?
15th May 2020, 6:45 AM
Alapottra Chakma
Alapottra Chakma - avatar
0
oh done..thank you
15th May 2020, 6:45 AM
Alapottra Chakma
Alapottra Chakma - avatar
0
btw this line name = str(input("name:")) even if user enters number it still executes without errors.. is there any way to let user put alphabets only?
15th May 2020, 6:47 AM
Alapottra Chakma
Alapottra Chakma - avatar
0
Try these. Input age() Age=int() Print(age)
16th May 2020, 7:57 PM
Damilare V. Oyeleye.
Damilare V. Oyeleye. - avatar