Hello!
I have this piece of code, which asks the user to continue with the program even if it may overwrite some folders.

print w.fill('WIP will create Backup, Resized and Watermarked folders to store the original, resized and watermarked pictures. It will overwrite them if they already exists. Are you sure you want to continue (y/n)?')
overwrite = raw_input('> ')
if overwrite == 'y':
    #Create a Backup directory
    if not os.path.exists('Backup'):
        os.mkdir('Backup')
    
    #Create a Resized directory
    if not os.path.exists('Resized'):
        os.mkdir('Resized')
    
    #Create a marked directory
    if not os.path.exists('Watermarked'):
        os.mkdir('Watermarked')
elif overwrite == 'n':
    print 'Thank you for using WIP'
    sys.exit()
else:
    print 'This option is not available'
    sys.exit()

I would like that the last option, which prints 'This option is not available', goes back to the beginning of this piece of code, asking the user for an input. How can I do this? I have thought of using

continue

, but I have read that it can only be used with the 'if' and 'while' statements.
Cheers!

Dani

Recommended Answers

All 6 Replies

One particularly neat solution is to put the code in a function...

def getYN(prompt='> ', errormsg='This option is not available.'):
    result = raw_input(prompt)
    if result == 'y' or result == 'n':
       return result
    else:
        print errormsg
        return getYN(prompt, errormsg)

This has the added benefit of crashing the program if the user refuses to enter 'y' or 'n' about 1000 times.

Something like this can do it also:

overwrite = None

print w.fill('WIP will create Backup, Resized and Watermarked folders to store the original, resized and watermarked pictures. It will overwrite them if they already exists. Are you sure you want to continue (y/n)?')
While overwrite.lower() != 'n' and overwrite.lower() != 'y':
    overwrite = raw_input('> ')
if overwrite == 'y':
    #Create a Backup directory
    if not os.path.exists('Backup'):
        os.mkdir('Backup')
    
    #Create a Resized directory
    if not os.path.exists('Resized'):
        os.mkdir('Resized')
    
    #Create a marked directory
    if not os.path.exists('Watermarked'):
        os.mkdir('Watermarked')
elif overwrite == 'n':
    print 'Thank you for using WIP'
    sys.exit()

You can also do this

print w.fill('WIP will create Backup, Resized and Watermarked folders to store the original, resized and watermarked pictures. It will overwrite them if they already exists. Are you sure you want to continue (y/n)?')
overwrite = raw_input('> ')
while overwrite not in ('y', 'n'):
    overwrite = raw_input("Please, answer 'y' or 'n': ")
if overwrite == 'y':
    # etc

Or you can do something like

while True:
    #...
    #...
    if overwrite = 'y':
        #...
        break
print w.fill('WIP will create Backup, Resized and Watermarked folders to store the original, resized and watermarked pictures. It will overwrite them if they already exists. Are you sure you want to continue (y/n)?')
while True:
    overwrite = raw_input('> ').lower()
    if overwrite == 'y':
        #Create a Backup directory
        if not os.path.exists('Backup'):
            os.mkdir('Backup')
     
        #Create a Resized directory
        if not os.path.exists('Resized'):
            os.mkdir('Resized')
     
        #Create a marked directory
        if not os.path.exists('Watermarked'):
            os.mkdir('Watermarked')
    elif overwrite == 'n':
        print 'Thank you for using WIP'
        sys.exit()
    else:
        print 'This option is not available. Try again [Y/N]'
        continue

It is good to lower the user input, however the last solution does not exit the loop in case of y answer.

My running code (normal print, imports, accepts yeah and nope):

import os

print('''
    WIP will create Backup, Resized and Watermarked folders to
    store the original, resized and watermarked pictures.
    It will overwrite them if they already exists.
''')

overwrite = ''
while not overwrite.startswith('y'): ## 'n' case will do exit
    overwrite = raw_input('Are you sure you want to continue (y/n)?').lower()
    if overwrite.startswith('n'):
        raise SystemExit, 'Thank you for using WIP'

#Create a Backup directory
if not os.path.exists('Backup'):
    os.mkdir('Backup')

#Create a Resized directory
if not os.path.exists('Resized'):
    os.mkdir('Resized')

#Create a marked directory
if not os.path.exists('Watermarked'):
    os.mkdir('Watermarked')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.