i am getting an error "Code is unreachable Pylance" what that mean or am i doing any mistake in my code?
Question:
def percent(marks):
return (marks[0]+marks[1]+marks[2]+marks[3]/400)*100
marks1=[54,65,85,54]
percent1=percent(marks1)
marks2=[54,52,65,85]
percent2 = percent(marks2)
print(percent1,percent2)
Answers:
The lines after return
will not be executed anytime. So you can delete them and nothing will change. The message told you about it, because it is very unusual to have such code.
I think you wanted this:
def percent(marks):
return (marks[0]+marks[1]+marks[2]+marks[3]/400)*100
marks1 = [54, 65, 85, 54]
percent1 = percent(marks1)
marks2 = [54, 52, 65, 85]
percent2 = percent(marks2)
print(percent1, percent2)
Spaces in Python code matter. In your code all lines are the part of the function. In fixed code they are not.
i know why code is unreachable its because the interpreter thinks your program has closed.
Something like exit()
what is under exit() the code interpreter thinks cannot be executed, But it can be executed!
For anyone coming across this Stack Overflow question wondering how to disable this feature in which Pylance greys out text which it (often mistakenly) thinks is unreachable, a solution is provided by luabud
in this comment and this comment on the GitHub issue How to disable "code is not reachable"…, which is to place the following entry in your user/workspace settings.json
file:
"[python]": {
"editor.showUnused": false,
},
def percent(marks):
return (marks[0]+marks[1]+marks[2]+marks[3]/400)*100
marks1=[54,65,85,54]
percent1=percent(marks1)
marks2=[54,52,65,85]
percent2 = percent(marks2)
print(percent1,percent2)
The lines after return
will not be executed anytime. So you can delete them and nothing will change. The message told you about it, because it is very unusual to have such code.
I think you wanted this:
def percent(marks):
return (marks[0]+marks[1]+marks[2]+marks[3]/400)*100
marks1 = [54, 65, 85, 54]
percent1 = percent(marks1)
marks2 = [54, 52, 65, 85]
percent2 = percent(marks2)
print(percent1, percent2)
Spaces in Python code matter. In your code all lines are the part of the function. In fixed code they are not.
i know why code is unreachable its because the interpreter thinks your program has closed.
Something like exit()
what is under exit() the code interpreter thinks cannot be executed, But it can be executed!
For anyone coming across this Stack Overflow question wondering how to disable this feature in which Pylance greys out text which it (often mistakenly) thinks is unreachable, a solution is provided by luabud
in this comment and this comment on the GitHub issue How to disable "code is not reachable"…, which is to place the following entry in your user/workspace settings.json
file:
"[python]": {
"editor.showUnused": false,
},