0

I'm currently writing a program about Currency Converter on Python and I'm in the last stage where I need help on a question where I ask "Do you want to convert again" and if the answer starts with "n" then I write a message and the program exits. Whereas the problem is that I want the program to restart if the user wants to convert again". I need to define how to restart the program but i'm not sure how. At the end its a else if the user writes anything else except "y" or "n" Help is much appreciated. So far it looks like this:

def restart_programme
answer=str(input('Do you want to convert again?'))
if answer.lower().startswith("n"):
    print("Thank you for using Currency Converter today,Goodbye "+name)
    exit()
if answer.lower().startswith("y") or ("Y"):
restart_programme()

3 Answers3

0

Enclose your entire program in a loop.

answer = 'y'
while answer.lower().startswith("y"):

    #Whatever you want to do

    answer=str(input('Do you want to convert again?'))

if answer.lower().startswith("n"):
    print("Thank you for using Currency Converter today,Goodbye "+name)
else:
    #The anything else part

OR

Enclose your entire program in a function and simply call that function again.

def restart_programme():

    #Whatever you want to do

    answer=str(input('Do you want to convert again?'))
    if answer.lower().startswith("n"):
        print("Thank you for using Currency Converter today,Goodbye "+name)
        exit()
    if answer.lower().startswith("y"):
        restart_programme()

restart_programme()
  • `if answer.lower().startswith("y") or ("Y"):` always evaluates to true. – Dair Jul 03 '14 at 22:17
  • but how can I define restart_program to go to the beginning of the whole program – user3802847 Jul 03 '14 at 22:19
  • You put your whole program into the restart_programme function. Then you call that function again and it reaches the beginning. Or you could put your entire program in a main() function and call that whenever you need to go to the beginning. – Abhinav2107 Jul 03 '14 at 22:27
0

python 2.7

def restart_programme():
    answer = raw_input('Do you want to convert again? [Y/n]\n')
    if answer.lower().startswith('n'):
        print('Thank you for using Currency Converter today, Goodbye ' + name)
        quit()
    elif answer.lower().startswith('y'):
        restart_programme()
        print ('Debug: Program will now restart\n\n')
    else:
        print('You didnt choose a valid option')
        raw_input('Press Enter to Restart\n')
        restart_programme()
KingMak
  • 1,378
  • 2
  • 12
  • 26
0

Really that no one who answered before tested their code? Both there are the same problem. You should read this

def got_milk():
    answer = False
    while not answer:
        milk = raw_input('Got Milk?')
        if not milk in ['N', 'No', 'n', 'no']:
            print 'Thanks'
            answer = True

This could be some version of what you expected to do:

def restart_programme():
    check = True
    while check:
        answer=raw_input('Do you want to convert again?').lower()
        if answer.startswith("n"):
            print("Thank you for using Currency Converter today, Goodbye")
            check = False
            exit()
        if answer.startswith("y") or answer.startswith("Y"):
            print "Let's have some more fun."
Community
  • 1
  • 1
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43