1

I've been trying to figure this out for some time now, and I still can't seem to get it! I am trying to return and print the correct number of rows and columns that a user enters (when a user enters greater than or equal to 5) and when the user doesn't enter, to prompt the input statement again.

Here is my code so far:

#CONSTANTS 
CON_NUM = int(5)

def correctInteger(row):
    #get valid row number:  
    while row < CON_NUM:
        row = int(input("Please enter a number bigger than or equal to 5: "))
        if row >= CON_NUM:
            return(row)
    if row >= CON_NUM:
        return(row)

def correctNum(cols):
    #get valid column number:  
    while cols < CON_NUM:
        cols = int(input("Enter a number bigger than / equal to 5: "))
        if cols >= CON_NUM:
            return(cols)
    if cols >= CON_NUM:
        return(cols)

def main():
    #ask for number of rows:
    print("Choose the number of rows:")
    rows = int(input("Please enter a number bigger/ equal to 5: "))

    #ask for columns:
    print("Please choose the number of columns:")
    columns = int(input("Please enter a number bigger/ equal to 5: "))
    validRow = correctInteger(rows)
    validColumn = correctNum(columns)
    print(validRow)
    print(validColumn)

main()

Here is the output this code makes:

Please choose the number of rows:
Please enter a number bigger/equal to 5: 3
Please choose the number of columns:
Please enter a number bigger/equal to 5: 5
Please enter a number bigger/equal to 5: 6
6
5

The output is very strange and I dont know how to fix it! I don't know why the while loop for the number of rows isn't working and it is only printing the columns.

Output that I am trying to get might look something like:

Please choose the number of rows:
Please enter a number bigger/equal to 5: 4
Please enter a number bigger/equal to 5: 5
Please choose the number of columns:
Please enter a number bigger/equal to 5: 3
Please enter a number bigger/equal to 5: 7
5
7

(top number being rows and bottom being columns)

I know this is really long but I hope I'm making sense! Thank you for your time!

0

2 Answers 2

1

Similar to @Prune's answer but with a slightly different construct that handles non-number entries. You have common requirement so factor it out and create a single function to do the validation, e.g. ask():

# ask for a number at least as big as n
def ask(n):
    while True:
        try:
            i = int(input("Please enter a number bigger than or equal to {}: ".format(n)))
            if i >= n:
                return i
        except ValueError:
            pass

def main():                                                                     
    #ask for number of rows:                                                                  
    print("Choose the number of rows:")
    rows = ask(5)

    #ask for columns:                                                               
    print("Please choose the number of columns:")
    columns = ask(5)
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

You've distributed your logic too widely to follow easily. You gather the input from two places for each of the row and column, and the you check it in three different places.

Simplify it something like this:

get_legal_row_limit():
    CON_NUM = 5
    row = int(input("Please enter a number bigger than or equal to 5: "))
    while row < CON_NUM:
       row = int(input("Please enter a number bigger than or equal to 5: "))
    return row

Now, your main program simply calls this with

rows = get_legal_row_limit()

... without further overhead. Do likewise for columns.

This is the basic "fetch me something useful" construct:

Get me a value
While the value is unacceptable
    Get me another value
Return the good value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.