0
why I'm getting is error ?
age = int(input('age: ')) print(age) output = IndentationError : unexpected indent
12 Antworten
+ 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 ...
+ 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')
+ 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.
+ 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
0
try:
age = int(input("age: "))
X= age
name = str(input("name"))
Y=name
expect : ValueError:
print("invaild value")
finally:
print (X, Y )
0
now it's saying undefined X Y
0
nameError: name 'X' is not defined
0
I presume <X> and <Y> are only available within the `try` block, where they were defined. You are using them in `finally` block.
0
any way to fix this ? should i remove finally block?
0
oh done..thank you
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?
0
Try these.
Input age()
Age=int()
Print(age)