-1

I've just started a pretty basic program in PyScripter using python3. I've asked the user to input a number and my program caculates the number. Thats all running well but I wondered if anyone know how to on python as the user if they want the program to run again or evit. If so how do I run the caculator again and how do I exit at there request. I.E "would you like to run the program again type yes or no* if yes how do I execute the same code again if no how to stop it from executing. Thanks fro your help.

Number = int(input("Number Required"))

 if Number >= 1 and Number<=50:
  add =(Number * 5)
  print("The Number amount it", add)

 elif Number <= 80 and Number >= 50:
   add =(Number * 4)
   print("The Number amount it", add)

 elif Number <= 100 and Number >= 80:
   add =(Number * 2.5)
   print("The Number amount it", add)

 run = input("would you like to check again type yes or no")
msw
  • 42,753
  • 9
  • 87
  • 112

4 Answers4

1

add all of your code inside a while loop

repeat = True
while repeat:
    Number = int(input("Number Required"))

    if Number >= 1 and Number<=50:
      add =(Number * 5)
      print("The Number amount it", add)

    elif Number <= 80 and Number >= 50:
       add =(Number * 4)
       print("The Number amount it", add)

    elif Number <= 100 and Number >= 80:
       add =(Number * 2.5)
       print("The Number amount it", add)

    run = input("would you like to check again type yes or no")
    if run == 'no': repeat = False

Small Tip

Instead of having a long comparison like Number >= 1 and Number<=50 you can instead do as Padraic suggested which is 1 <= Number <= 50. It is easier and more readable.

Community
  • 1
  • 1
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

Once a process finishes it cannot start itself - it needs another process or service to start it again but from how you presented your issue you just need to loop over the some logic without exiting the process.

Updating my answer as you updated the question

while True:
    x = input("Enter your input: \n")
    some_logic_here()
matcheek
  • 4,887
  • 9
  • 42
  • 73
0

I wrote a simply calculator program that has the kind of function I believe you're looking for here: https://github.com/ArnoldM904/Random_Programs/blob/master/Python_Programs/BlackBox_Area%26Volume.py

To keep it simple though one (of many options) would be to make a basic Restart() function.

The Restart() function I call after a calculation in my example:

def Restart():
    toMenu = raw_input("\nTo go back to main menu, type '1'. To exit, type '2'.\n\n")
    if toMenu == '1':
        menu() # My menu function
    elif toMenu == '2':
        exit() # Built-in Python Exit-program option
    else:
        invalid() # My "Invalid input" error message
Vale
  • 1,003
  • 1
  • 9
  • 22
0

Have a single prompt that covers all the possibilities

while True:
    answer = input("Enter number or 'q' to exit")
    if answer == 'q':
        break
    try:
        Number = int(answer)
    except ValueError:
        print("'{}' is not a number".format(answer))
        continue
    ... your calculator here ...
tdelaney
  • 73,364
  • 6
  • 83
  • 116