+ 2
What is wrong with this code...? 🤔
i=int(input("ENTER") if(i>18): print("you are eligible to vote") else: print("you are not eligible")
14 Antworten
+ 9
You missed the small bracket in input and indentation.
For instance:
i=int(input("ENTER"))
if(i>18):
print("you are eligible to vote")
else:
print("you are not eligible")
You have to close the small bracket and fix the indentation.
I hope I answered your question.
+ 7
Yashobanta Behera ,
if you are using the code on sololearn with code coach, you should remove the `prompt` inside the parenthesis of the input function.
+ 5
Yashobanta Behera ,
People have already answered the syntax errors.
I just want to urge you to adopt the convention of consistently typing four spaces per indentation level.
Single space is hard enough for others to read in the monospace font of the Code Playground, but it's ridiculous in the proportional font of the comments engine, where spaces are half the width of a normal character.
It might also help you avoid making future indentation errors when you can better see your own indentation.
+ 2
age = int(input("Enter age: "))
if age > 18:
print("Eligible for Voting!")
else:
print("Not Eligible for Voting!")
This works.
I used Google bard for writing
And it gave me this
I think the problem was by ()
+ 2
Stuart Hillman ,
From what I learned from Harvard's free CS50 with Python video on YouTube, using a raw except (which catches every exception) in a loop prevents the user from escaping the loop with Ctrl-C, because Ctrl-C generates an exception, which the raw except catches before continuing anyway.
It's better to explicitly name the exception you expect, which I think would be ValueError when int() receives "Hi!" or something, before printing the usage, so other exceptions are not inadvertantly pulled into the same clause and handled the same way.
except ValueError:
# print usage here
continue
And of course, on Sololearn, there's no interactive input, and programs are automatically halted if they run longer then a few seconds. The user is required to enter all inputs before the program runs. So the loop idea is only for running elsewhere.
+ 1
bracket missing on line 1
+ 1
I would also consider adding a simple exception handler to take account of incorrect entry by the user, like this:
while True:
try:
i=int(input("ENTER AGE: "))
break
except:
print("Invalid age. Enter whole numbers only.")
continue
if(i>18):
print("You are eligible to vote")
else:
print("You are not eligible")
+ 1
You need to include one more bracket after "ENTER". Coz you have only closed the statement parenthesis and not the input one. So just add one more bracket.
+ 1
syntax error and a missing parenthesis in your code. You're missing a closing parenthesis in the first line after `input("ENTER")`. It should be `i=int(input("ENTER"))`. Shakah thinks
+ 1
Rain good point, I'm still learning too!
I also failed to realise it was a specific sololearn playground example and not a generic question. My mistake also.
Thanks for the tips!
+ 1
Yashobanta Behera ,
For example, this is an indentation error (unexpected indent).
x = 10
y = 20
And this is its correction.
x = 10
y = 20
And for example, going the other way, this is an indentation error (unexpected unindent).
for index in range(5):
print(index)
And this is its correction.
for index in range(5):
print(index)
+ 1
Yashobanta Behera Indentation is how python determines which lines of code form part of expressions or functions. Often languages use braces ( { } ) to identify code "within" something, while python relies on intentation.
All code which forms part of the same thing must have the same level (number of spaces) of intentation. The actual level is not important, but it is typically accepted that multiples of 4 creates clear and easy to read code. The example below demonstrates this using stars for illustration only. The code is garbage but it illustrates the point.
x=0
run = True
While run:
****x +=1
****for i in range(10):
********y = x+i
********print(y)
****if day == Tuesday
********run = False
print('Have a nice day')
0
bracket missing
0
Can anyone briefly explain what Indentation Errors are?