Hi *
I'm trying to make a program that converts a vertical list to a horizontal one which works. But am trying to build in check for some correct user input an I get an Indentation error

File ".\dairymaster.py", line 19
pn_test = check_input('1', '2')
^
IndentationError: expected an indented block

If I run this piece of code seperate it works fine but combination with the rest I get the above error

import sys

action = "draft"

#print ('Please enter which pen:')
#pn = raw_input()
def check_input(a, b):
    """
    check the input 
    """
    prompt = "Please enter which pen %s or %s: " % (a, b)
    while True:
        try:
            pn = str(raw_input(prompt))
            if pn == '1' or pn == '2':
                return pn
        except StringError:

pn_test = check_input('1', '2')
print(pn_test)

#print ('Please enter AM or PM:')
#moa = raw_input('Please enter AM or PM:')
def check_input(c, d):
    """
    check the input 
    """
    prompt = "Please enter either %s or %s: " % (c, d)
    while True:
        try:
            moa = str(raw_input(prompt))
            if moa == 'am' or moa == 'pm':
                return moa
        except StringError:

moa_test = check_input('am', 'pm')
print(moa_test)

#print ('Please enter how many days from now, enter = 0:')
#dfn = raw_input()
def check_input(e, f):
    """
    check the input 
    """
    prompt = "Please enter either %s or %s for today or tomorrow: " % (e, f)
    while True:
        try:
            dfn = str(raw_input(prompt))
            if dfn == '0' or dfn == '+1':
                return dfn
        except StringError:

dfn_test = check_input('0', '+1')
print(dfn_test)

print ('Please enter the filename:')
fname = raw_input()


handle = open(fname, "r")
list = handle.readlines()

combine = ''.join(list).replace("\n", " ")

if dfn == '0'
    return dfn == ' '

oname = "copyme.txt"
fout = open(oname, "w")
fout.write(action + pn + moa + ' ' + dfn + ' ')
fout.write(combine)
fout.close()

Recommended Answers

All 4 Replies

Indent the lines inside the try's except block, same level as the dfn = ... line 48, but I would like you to generalize the check in function, like I did in my code snippet. You are redefining the check_input three times so it is better to remove lines 7...40 which does nothing. Also definition of line 3 is never used anywhere.

Thanks for that. I will review and use your snippet as a guide

The definition in line 3 is used in line 70 when the output file gets created

If they do different things they probably need their own names. pyTony is suggesting you don't redefine the same function more than once. Since they do almost the same thing, they could probably use a single function with different parameters.

def check_input(options, msg):
    """ checks input, accepts a number of possible answers. """
    # options is a list or tuple with acceptable items (first_item, second_item) or [first,second]
    # this would allow you to expand your possible answers like:
    # check_input(('1', '2', '3'), 'Select %s, %s, or %s: ')
    prompt = msg % (options)
    while True:
        try:
            dfn = str(raw_input(prompt))
            if dfn in options:
                return dfn
        except StringError:
            print "Please enter a valid answer."
        except KeyboardInterrupt as excancel: #@UnusedVariable
            print "User cancelled, goodbye.\n"
            exit(1)

pin = check_input(('1', '2'), "Please enter which pen, %s or %s: ")
moa = check_input(('am', 'pm'), "Please enter the time of day, %s or %s: ")
dfn = check_input(('0', '+1'), "Please enter %s for today, or %s for tomorrow: ")

See, one function that does it all, since they do almost the same thing.
Also, on line 65: you are doing this:

if dfn == '0'          # <- missing :
    return dfn == ' '  # <- will always return False because "dfn == ' '" is False (dfn == '0').

# but you are returning outside of a function?

Were you trying to do this?:

if dfn == '0':
    dfn = ' '  # <- assignment, not evaluation..

Hi thank you (sorry did not reply earlier but I don't get a email that there is a reply)
That makes it a lot easier and shorter

For your last comment about dfn: if it is 0 there has to be a space instead otherwise I'll get an error, the output gets copied into another program that is very strict about the format of the input.

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.