+ 6
Why do it not show an error?
https://code.sololearn.com/csOFXD12qI6R/?ref=app Please tell why it has no output without error
20 Answers
+ 14
When you compile a Python code, it's basically only checked for correct syntax.
If a name exists, will only be checked at runtime.
So if you wrote range(1, 10), the loop would execute...
... and only then you would get the error, because i is not defined.
You can apply that to other situations as well.
For example try these lines of code:
if True or hellohooo:
print('Hello', end=' ')
if False and anybodyThere():
print('world')
This throws no error, because both the variable hellohooo and the function anybodyThere are never even reached.
+ 12
Prince Yadav Because range is 10 to 1 not 1 to 10. So it will not go inside loop.
+ 9
Since your start value is bigger than end value your for loop won't execute even once. Otherwise an error would occur, because you have not defined i in print(i)
+ 8
@ace, that makes me think of....
Absence of proof is not proof of absence.
+ 7
Code Crasher, it is not really about for loops, but a separate issue.
Non-dynamic languages usually give you a bit more, so for example in C you get warnings like:
'Hey, dude, you declared a variable and didn't even use it!'
Python doesn't care - it's all checked at runtime.
See, when you define a function...
def f(x):
doStuffWithX(x)
... you should get an error, right? Because x isn't even defined yet. Because the function hasn't been called.
You don't get an error though, because the function isn't even called, so the code in there not executed, so the check for x never happens, so the issue of x not being defined never actually comes up.
So this hasn't anything to do with for loops - it is about a generally valid block of code, that is just never reached.
The simplest example (try it in Playground, it works):
print('Hello')
quit()
lalalalala
+ 5
Well, Ace, isn't that a good thing?
I mean, like when you go to a doctor and they found nothing, it's a 'negative', but positive for you?
+ 5
Sure thing, Ace - there's no 'just reinstall everything' with a human being (at least not until we learn how to make a data backup - but that's quite Star Trek now 😅).
What I meant was this:
You have to test for all imaginable bugs as a developer, right? No way around it.
And if you don't find a bug, you can only assume there aren't any... until you're proven wrong.
+ 4
I guess that is one of the hardest and most important lessons every programmer has to learn:
Test, debug, test, debug, test, debug, test, debug... until you're 💯% sure everything works as expected.
Then give it to people and wait for the bug reports.
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥, that's the point. We're mere humans after all. Usually we go for 110 and achieve 95 - if we're trying really hard.
+ 4
Ace, I suppose so... I was afraid I missed something. 🤔😉
+ 2
Sohan kinage for loop Wil still work perfectly range (10,1) but you must not forget the step argument which must be negative integer in this case.
Range(10,1,-1) works perfectly
+ 2
i is not defined if at all it was to run. Otherwise range(10,1) doesn't sense.
You can try
for _ in range(10):
print(_)
+ 1
good answer AJ Anant
+ 1
for i in range(10,1,-1):
print (i)
This is the corrected code I made.
the problem is you did not specify the step argument.
when you are counting from a max to a lower Bond you must explicitly spercify step argument of range () which must be negative. So that the for loop will count from the top to down. In this case from 10 to 1.
Hope this helped.
+ 1
Range should be (1,10) ;
there are three parameters for range
Eg. Range(1,10,2)
1 represent start with
10 represent end with
2 represent. Step by
When you put range(10,1) it will not accept the value
Hence for loop will not give any output...
+ 1
You need to change to range(1,10)
And you must define for i so that you can print i
0
It's all bcz,actually the for loop range can be either from (1-10) or from (10-1) but all u need to do is either increment or decrement value of i.In the case of (1-10) it is not mandatory but,we need to mention decrement of i in case of (10-1).
Try it and run the code again....I hope ur issue will be resolves
0
#try this
for _ in range(1,10):
print (_)
0
Ptar* u said is correct but u need to also define step and it should be like this👇
for i in range(10,1,-1):
print( i )
0
Why is my code being marked incorrect in Python beginners course Chatbotv1.1?