Avinab Rout
Avinab Rout

Reputation: 23

break outside the loop error in python

I'm created simple bank programme where i create four methods for the transaction. below is my code. the problem is it shows an error that "break outside the loop". kindly help, I'm new to python.

bal=0
def deposit():
    global bal
    amount=input('Enter Deposit Amount: ')
    bal=bal+amount

def withdraw():
    global bal
    amount=input('Enter Withdraw Amount: ')
    bal=bal-amount

def checkbal():
    global bal
    print bal

def conti():
    c=raw_input('Do You Wana Continue y/n....')
    if c=='y':
       main()
    else:
       break   

def main():
    print '---Welcome To ABC Bank---'
    print 'Enter 1 For Deposit:'
    print 'Enter 2 For Withdraw:'
    print 'Enter 3 For Balance Check:'
    print 'Enter 4 For Exit:'
    choice= input('Enter Your Choice :')

if(choice==1):
    deposit()
elif(choice==2):
    withdraw()
elif(choice==3):
    checkbal()
else:
    print 'Invalid Entry'

conti()

main()

Upvotes: 1

Views: 3556

Answers (2)

skaul05
skaul05

Reputation: 2344

def conti():
  c=raw_input('Do You Wana Continue y/n....')
  if c=='y':
    main()
  else:
    break

You can use return statement in place of break because break will come outside the loop whereas return will return you to main function else if you want to come out of program you can simply use exit()

Upvotes: 0

malioboro
malioboro

Reputation: 3291

break outside the loop

it means you use break not inside a loop. break is only used inside a loop when you want to stop the iteration.

So, in this code:

def conti():
    c=raw_input('Do You Wana Continue y/n....')
    if c=='y':
       main()
    else:
       break  # should be exit()

If you want to exit from program if user choose not to continue, then break should be exit()

or return if you just want to exit from conti() function. (But it means you still go to main() function)

Upvotes: 2

Related Questions