Home > Software engineering >  i am having problems in doing a loop in python
i am having problems in doing a loop in python

Time:06-27

so I'm making a program that needs to pick a number (like 10) then add a loop that adds one unit to my number Everytime. I have tried this :

while true :
    num = 10
    num  = 1
    print(num)

can you help me on this? thanks.

CodePudding user response:

Your example won’t work because you are resetting it to 10 every time.

You have to do so something like this:

num = 10 #Set it to 10 once
while True:
   num  = 1 #add 1 each time
   print(num)

Note that true is invalid and in python you use True. Also note that python is based on whitespace so you will Never use (bool) in a if statement or a loop. It is just a waste of space to add ()

CodePudding user response:

Try doing

while (true):
    num = 10
    num  = 1
    print(num)
  • Related