|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
newbie having trouble - defining
how do I define 'while true' in this program.
Code:
count = 0
while true:
count += 1
if count > 10:
break
if count == 5:
continue
print count
raw_input('press the enter key to exit')
# this program is suppose to count from 1 to 10 and skip 5 ![]() Last edited by netytan : April 30th, 2004 at 11:41 PM. |
|
#2
|
|||
|
|||
|
The while statement will loop as long as the expression next to it is true. Using the word "True" is like using while 1:, or in other words, it will loop forever. You don't need to define the "True" constant, because it is built into Python, so just use "True".
To answer the question in your other thread: The variable "count" is used to count the amount of interations, and once it passes 10, the loop exits. This information is all avaible on Python's website, so please have a look: Python.org Last edited by NetBSD : April 30th, 2004 at 11:35 PM. |
|
#3
|
||||
|
||||
|
A better way to do this would be to use a while expression instead of True i.e.
Code:
count = 1
while count <= 10:
if count != 5:
print count
count = count + 1
raw_input('press the enter key to exit')
you can also use a for loop for this simple task. Code:
for number in range(1, 10): if number != 5: print number In this instance using a while True loop is a waste of time but its up to you. Hope this helps Mark. |
|
#4
|
|||
|
|||
|
Thanks netytan you've been a great help, your code worked.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > newbie having trouble - defining |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|