Why do it not show an error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why do it not show an error?

https://code.sololearn.com/csOFXD12qI6R/?ref=app Please tell why it has no output without error

17th Jun 2020, 1:07 PM
Prince Yadav
Prince Yadav - avatar
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.
17th Jun 2020, 1:37 PM
HonFu
HonFu - avatar
+ 12
Prince Yadav Because range is 10 to 1 not 1 to 10. So it will not go inside loop.
17th Jun 2020, 1:21 PM
A͢J
A͢J - avatar
+ 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)
17th Jun 2020, 1:21 PM
Amar Samdan
Amar Samdan - avatar
+ 8
@ace, that makes me think of.... Absence of proof is not proof of absence.
17th Jun 2020, 7:57 PM
Louis
Louis - avatar
+ 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
17th Jun 2020, 2:14 PM
HonFu
HonFu - avatar
+ 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?
17th Jun 2020, 4:00 PM
HonFu
HonFu - avatar
+ 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.
17th Jun 2020, 4:26 PM
HonFu
HonFu - avatar
+ 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.
17th Jun 2020, 3:23 PM
HonFu
HonFu - avatar
+ 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.
17th Jun 2020, 3:59 PM
HonFu
HonFu - avatar
+ 4
Ace, I suppose so... I was afraid I missed something. 🤔😉
17th Jun 2020, 5:39 PM
HonFu
HonFu - avatar
+ 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
18th Jun 2020, 1:38 PM
MILES CODER
MILES CODER - avatar
+ 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(_)
19th Jun 2020, 9:08 AM
Ptar*
Ptar* - avatar
+ 1
good answer AJ Anant
18th Jun 2020, 12:37 AM
Thecouchcushion
Thecouchcushion - avatar
+ 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.
18th Jun 2020, 12:56 PM
MILES CODER
MILES CODER - avatar
+ 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...
18th Jun 2020, 1:33 PM
Sohan Kinage
Sohan Kinage - avatar
+ 1
You need to change to range(1,10) And you must define for i so that you can print i
19th Jun 2020, 1:33 AM
Man Ip Chung
Man Ip Chung - avatar
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
18th Jun 2020, 10:29 AM
Vishnu_kishore@473
Vishnu_kishore@473 - avatar
0
#try this for _ in range(1,10): print (_)
19th Jun 2020, 7:44 AM
Darshan Thapa
Darshan Thapa - avatar
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 )
20th Jun 2020, 7:59 AM
Vishnu_kishore@473
Vishnu_kishore@473 - avatar
0
Why is my code being marked incorrect in Python beginners course Chatbotv1.1?
14th May 2023, 9:07 AM
Keturah Cutting
Keturah Cutting - avatar