A program can be looped in Python by wrapping the entire program in a for or while loop.
If you wish to loop the program a finite amount of times, it can be done like so (x = the amount of times you want it to loop, this can be a number or variable storing a number):
for i in range(0,x):
[code]
If you wish to loop the program infinitely, you can use a simple while loop:
while True:
[code]
a = 0while a < 10 :a += 1print (a)Write the above simple script in a text editor (I use nano). Save as loop.py in home folder. To run, open a terminal and at the prompt, write: python loop.pyResult:rodney@downstairs:~$ python loop.py12345678910
Python is just a programming software, you don't actually need it to run your computer. You can use Python if you'd like to program something, or learn how to program.
Infinite loop.
open MS-DOS in the directory you have the python file in. type "python [INSERTNAMEOFSCRIPT]"
12345 1234 123 12 1
An infinite loop might look something like: while 1==1: print("Infinite loop") as 1 is ALWAYS equal to 1.
A Python loop is something that will always happen or continue to happen until the condition isn't met. So for example:while 1==1:print("Infinite loop")would be an infinite loop, as 1 will ALWAYS be equal to 1.
Here's the code for your program: # Loop through the uppercase alphabet letters for letter in range(ord('A'), ord('Z') + 1): print(chr(letter)) BTW you can use this code in Python and try it out for yourself.
this is the program I'm trying to loop #Speed Speed =input('Speed in MPH:') #Distance Time= input('Time in hours:') # floating point number Speed = float(Speed) # floating point number6 Time = float(Time) #Calc Distance=Speed*Time
a = 0while a < 10 :a += 1print (a)Write the above simple script in a text editor (I use nano). Save as loop.py in home folder. To run, open a terminal and at the prompt, write: python loop.pyResult:rodney@downstairs:~$ python loop.py12345678910
Wesley Chun has written: 'Python fundamentals' -- subject(s): Python (Computer program language) 'Core Python programming' -- subject(s): Python (Computer program language)
Python is just a programming software, you don't actually need it to run your computer. You can use Python if you'd like to program something, or learn how to program.
Infinite loop.
open MS-DOS in the directory you have the python file in. type "python [INSERTNAMEOFSCRIPT]"
odd loop means at least the loop execute once.
Python is a runtime interpreter. It usually runs on the web server.
In python 3 I would write the following (assuming the digits are input by a user): #!/usr/bin/python a = input("Type a number: ") sum = 0 for i in a: #the for next loop performs the function of a while loop with less lines of code. #(My formating keeps getting deleted when I publish this answer. There should always be a tab- strictly speaking four spaces before this line) sum = sum + int(i) sum = str(sum) print("Your number was", a + ". The sum of its digits is" , sum + ".") As you can see, the for next loop iterates through each value in the string "a", and sets its value to "i". For each iteration, the current value of i is added to the variable "sum" until the end of the number is reached. The program then prints the original number, and the sum of its digits.