0

I am fairly new to programming and I scrapped some code together to make a sequence calculator using python. I'm trying to restart it at the "user_continue = input ("Would you like restart [y/n]? ")" part whenever the user would input an invalid answer, but I don't know how to do that, help?

import time
from time import sleep
while True:

def sumOfAP( a, d,n) :
    sum = 0
    i = 0
    while i < n :
        sum = sum + a
        a = a + d
        i = i + 1
    return sum

numsterm = int(input("Enter Number OF terms: "))
firstterm = int(input("Enter First Term: "))
difference = int(input("Enter The Difference: "))
print (sumOfAP(firstterm, difference, numsterm))
#   restart here
user_continue = input ("Would you like restart [y/n]? ")
if user_continue == ('y'):
  print("Continuing...")
  sleep(0.5)
elif user_continue == ('n'):
    print ("Thank you for using this program")
    print ("")
    print ("-PettyRap")
    sleep(2)
    break

else:
    print("Error Command Not Found")
    ???
  • 1
    Can you clarify what do you mean by restarting? Do you want the user to input new numbers or do you want to print the answer again? – Sami Jan 09 '22 at 13:39
  • I would like to re-ask the question "Would you like restart [y/n]?" if they input an invalid answer – PettyRap Jan 09 '22 at 13:44

1 Answers1

0
import time
from time import sleep
def sumOfAP( a, d,n) :
    sum = 0
    i = 0
    while i < n :
        sum = sum + a
        a = a + d
        i = i + 1
    return sum

def takeInputs():
    numsterm = int(input("Enter Number OF terms: "))
    firstterm = int(input("Enter First Term: "))
    difference = int(input("Enter The Difference: "))
    print (sumOfAP(firstterm, difference, numsterm))
#   restart here
takeInputs()
while True:
    user_continue = input ("Would you like restart [y/n]? ")
    if user_continue == ('y'):
      takeInputs()
      print("Continuing...")
      sleep(0.5)
    elif user_continue == ('n'):
        print ("Thank you for using this program")
        print ("")
        print ("-PettyRap")
        sleep(2)
        break

    else:
        print("Error Command Not Found")
Dharman
  • 30,962
  • 25
  • 85
  • 135