2019-05-08 05:24:25 (edited by Krantikari 2019-05-08 05:41:15)

Hi everyone I am a beginner in python.
I am trying to code a simple script which counts 1 to 10 leaving 5 but i am  getting this error:
>>> count = 0                                                                                                           
>>> while true:                                                                                                         
...     count += 1                                                                                                     
...     if count > 10:                                                                                                 
...        break                                                                                                       
...     if count == 5:                                                                                                 
...         continue                                                                                                   
...     print(count)                                                                                                   
Traceback (most recent call last):                                                                                     
  File "<stdin>", line 1, in <module>                                                                                   
NameError: name 'true' is not defined                                                                                   
>>> input("\nPress the enter key to exit.")                                                                             
Press the enter key to exit.
I have take example of other code as well it works fine but the code that  i typed does not work fine.
why it is happening
i am using edsharp to type these code.
i am using python
3.7.2
in windows 10 with nvda
below  i have posted my own code and the code from which i took reference
my own code::
count = 0
while true:
    count += 1
    if count > 10:
       break
    if count == 5:
        continue
    print(count)

input("\nPress the enter key to exit.")
reference code::
count = 0
while True:
    count += 1
    if count > 10:
       break
    if count == 5:
        continue
    print(count)

input("\n\nPress the enter key to exit.")
please help.
due to these errors i am not being able to learn. python

thanks and regards.
Krantikari
visit my Youtube channel for technical related stuff

2019-05-08 06:12:10 (edited by Ethin 2019-05-08 06:14:27)

You must capitalize true and false, as well as other constants. (In other libraries and, indeed, in Pythons standard library, many constants are all uppercase; but in the world of Python, true and false are capitalized, not all uppercase.) Remember that Python is not case insensitive, meaning that true and True are entirely different variables. If you wish to use Python, you must learn to determine what case something is written in, else you won't get very far. Also, your loop can be easily rewritten to take up a few less lines:

count = 0
while count < 11: # not 10, because we're counting from zero, which is mathematically correct (there are debates on this subject)
  if i == 5:
    continue
  count += 1
print (count)
input("Press enter to exit")
"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-05-11 21:39:44

I'd advise using a for loop here. While loops are really meant for iterating an indefinite number of times, which is usually good for things like input validation. If you know the number of times you want code to run, use a for loop.
I strongly advise this if you are new to programming. It's easy to get stuck in an infinite loop when using the while structure. It's important that you understand the difference between the different types of loops earlier on in your understanding of programming concepts. Your code would look more like this:

for count in range(1, 11):
    if count == 5:
        continue
    print(count)

input("Press enter to exit")

This block of code

  1. Guarantees the number of times the code iterates

  2. Eliminates the need for a counter variable

  3. Eliminates the need to break from the loop

  4. Shortens the amount of code to accomplish the thing you want to do

What game will hadi.gsf want to play next?

2019-05-11 22:44:46

@ethin that code is wrong. You use if i == 5, however i isn't declared anywhere and you should use count for that. Also you increment count after the if condition with the continue, so it will loop forever when count is 5 because count never increases. Your loop will also count 11, because when count is 10 it still triggers the loop.

You could code it like this
count = 0
while count < 10:
    # Do this to avoid it printing 11
    count += 1
    if count == 5:
        continue
    print(count)

input("Press enter to exit")

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2019-05-11 23:22:07

@4, I completely missed that, apologies.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github