How to restart script based on user input

92 Views Asked by At
import time
import sys
pin = "5327"
balance = "67.32"
pinattempt=input("Please enter pin:  ")
if pinattempt==pin:
    depositORwithdraw=input("Please press 1 to deposit cash, 2 to withdraw cash or 3 to check balance.  ")
    if depositORwithdraw=="1":
            depositvalue=input("How much cash would you like to deposit?  ")
            print("Processing.")
            time.sleep(0.5)
            print("Processing..")
            time.sleep(0.5)
            print("Processing...")
            time.sleep(0.5)
            print("Processing.")
            time.sleep(0.5)
            print("Processing..")
            time.sleep(0.5)
            exit("   Succesfully deposited £" + depositvalue + " into account")
    elif depositORwithdraw=="2":
            withdrawamount=input("How much cash would you like to withdraw? ")
            if withdrawamount < balance:
                print("Successfully withdrawn £" + withdrawamount + " from your account. Please take your cash")
                time.sleep(3)
                exit("")
            if withdrawamount > balance:
                exit("You cannot withdraw more cash than is in your account (£67.32), please re enter your pin to continue.")
    elif depositORwithdraw =="3":
        carryon = input("You currently have £" + balance + " available in your account. If you would like to withdraw or deposit cash please press 1, if you would like to exit please press 2.")
        if carryon == "1":
            #restart script here
        elif carryon == "2":
            exit("Thank you, please don't forget to take your card.")
else:
    exit("Incorrect pin.")

when i do return i get: 'SyntaxError: 'return' outside function' i have tried with 'def name():' and it still hasn't worked 'continue' also does not work 'SyntaxError: 'continue' not properly in loop'

1

There are 1 best solutions below

0
On

You could wrap code in While loop and use go_ahead:boolean variable to control the loop. So using your example:

import time
import sys
pin = "5327"
balance = "67.32"
go_ahead=True
pinattempt=input("Please enter pin:  ")
if pinattempt==pin:
    while go_ahead:
        depositORwithdraw=input("Please press 1 to deposit cash, 2 to withdraw cash or 3 to check balance.  ")
        if depositORwithdraw=="1":
            depositvalue=input("How much cash would you like to deposit?  ")
            print("Processing.")
            time.sleep(0.5)
            print("Processing..")
            time.sleep(0.5)
            print("Processing...")
            time.sleep(0.5)
            print("Processing.")
            time.sleep(0.5)
            print("Processing..")
            time.sleep(0.5)
            exit("   Succesfully deposited £" + depositvalue + " into account")
        elif depositORwithdraw=="2":
            withdrawamount=input("How much cash would you like to withdraw? ")
            if withdrawamount < balance:
                print("Successfully withdrawn £" + withdrawamount + " from your account. Please take your cash")
                time.sleep(3)
                exit("")
            if withdrawamount > balance:
                exit("You cannot withdraw more cash than is in your account (£67.32), please re enter your pin to continue.")
        elif depositORwithdraw =="3":
            carryon = input("You currently have £" + balance + " available in your account. If you would like to withdraw or deposit cash please press 1, if you would like to exit please press 2.")
            if carryon == "1":
                #restart script here
                go_ahead = True
            elif carryon == "2":
                exit("Thank you, please don't forget to take your card.")
                go_ahead = False
else:
    exit("Incorrect pin.")