1

The only problem I am facing is with the assert statement. I need to make sure that the user enters only integers greater than zero and not any characters or symbols.The assert statement should print the error statement if the user enters a character or symbols.I have already tried isinstance(), type(variable) == int but I only get a name error. Is there a way without using if?

def PascalTriangle(rows):

    ptriangle = [[1], [1, 1]]
    if rows == 1:
        return ptriangle[0]
    else:
        for rownumber in range(2, rows):
            ptriangle.append([1]*rownumber)
            for number in range(1, rownumber):
                ptriangle[rownumber][number] = (ptriangle[rownumber-1][number-1]+ptriangle[rownumber-1][number])                           
                ptriangle[rownumber].append(1)
        return ptriangle

def PrintPascalTriangle(ptriangle):

    largest_element = ptriangle[-1][len(ptriangle[-1]) // 2]
    element_width = len(str(largest_element))
    def format_row(row):
        return ' '.join([str(element).center(element_width) for element in row])
    triangle_width = len(format_row(ptriangle[-1]))
    for row in ptriangle:
        print(format_row(row).center(triangle_width))

rows = input('Enter the number of rows in Pascal`s Triangle: ')
assert (rows > 0),"Number of rows should be greater than zero"
PrintPascalTriangle(PascalTriangle(rows)) 
Tigran Saluev
  • 3,351
  • 2
  • 26
  • 40
  • Possible duplicate of [Python input() error - NameError: name '...' is not defined](http://stackoverflow.com/questions/21122540/python-input-error-nameerror-name-is-not-defined) – Łukasz Rogalski Oct 22 '15 at 22:53

2 Answers2

1

.isdigit() checks to see if the input can be a positive integer. the while not puts the user into a loop until he enters a positive integer.

rows = raw_input("Question")
while not rows.isdigit():
    print"That is not a positive int!"
    rows = raw_input("Question")
rows = int(rows)
0
rows.isdigit()

Is a good way to start, handles non negative integers, so '2'.isdigit() would resolve true

I would parse it something like this:

rows = '1'

if (rows.isdigit() and int(rows) > 0):
    print("success")
else:
    print("fail")

Checked works on inputs '1' and 'a', with return proper results. Remember that the logical operations parses left to right, so if it's not a digit, the second part int(rows) > 0 will not be executed.

Happy coding

mrhn
  • 17,961
  • 4
  • 27
  • 46
  • It's returning Name Error when I enter a character.I need the program to print the custom error message stated in the assert statement. – Sashreek Reddy Oct 22 '15 at 22:35
  • Updated it, with an example parsing i would do – mrhn Oct 22 '15 at 22:44
  • name errors often accompany variables that haven't been properly defined. Is it possible you have a problem with your code structure (indentation, blocks)? What is the rest of the error message? name 'rows' is not defined? – Dave Oct 22 '15 at 22:45
  • @Mr. Dave The program is running successfully for all values of int.The only problem is when I enter a character,I am getting the Name Error.But I want the program to print the error message given in the assert statement when I enter a character. – Sashreek Reddy Oct 22 '15 at 22:57
  • Which functional call generates the error? Take a look at the line numbers in your traceback to determine this. – Dave Oct 22 '15 at 23:01