Forum Archive

Getting program to run multiple times

DaveClark

I am doing some calculation work and I have something that works, but I am having trouble making it run multiple times. I would like to run a bunch of different values but at the moment it only runs once in the console and kicks you out. How can I change this to run until the user stops the program. Thanks for any help

```
import math

def main():
try:
bump=float(input('\nbump height? '))
tire=float(input('tire diameter in inches? '))
speed=float(input('vehicle speed in miles per hour? '))

    except ValueError:
            print('\nyou entered an invalid input')
    else:

          print('the hub speed is ' + repr(round((calculatehubspeed(bump, tire, speed)))) + ' inches per second')

def calculatehubspeed(bump, tire, speed):

    answer = bump/(math.sqrt((tire/2)**2-((tire/2-bump)**2))/(speed*17.6))

    return(answer)

if name == 'main':

main()

            ```
dgelessus

It should be enough to put your entire main code into a while True loop. That way it will repeat forever (until you press the stop button - that raises an exception, which stops the loop).

Drizzel

Put main() into a while True or for x in range() loop.

#your definitions and so on...

While True:
  main() #the indented part is now repeating for eternity... :-) if you wish to stop the loop, just insert the command break 

The for x in range(range) loop will repeat for x times whatever integer the range variable is set to.

for x in range(12):
  print(x) # x is equal to the number of times that the loop has been executed already
  main() # now your code is repeated 12 times
DaveClark

Thanks for helping me. It took about 10 seconds to change it after I read about the While loop. I was tying to put the main function in the loop, which was wrong. I needed to put the main function call in the While loop.
I have a bunch of calculations for use in the console but I want to put them into some kind of menu and page (with textboxes for answers) app so there can be a number keypad for ease of use

Again, this forum never lets me down, Thanks guys and gals