samedi 28 septembre 2019

Putting multiple variables into a IF statement and loop

I just started my IT studies in France, and they're moving really fast. From the first lesson, I already had to write some codes for homework and I can't seem to get it right.

The task: To print out a table (made of characters, so basically _ and |) on IDLE Python (Python Shell 3.7.4). The user must be asked for the number of rows and columns at the start of the program. The user must be asked if they wish to restart the program or not.

The problem: I have successfully done the first part of the task (printing the table). However I have failed to put in "fail proofs".

Basically I would like to do it that if the user types in something OTHER than a integer at the start of the program, he will be asked to enter only numbers and asked to enter the rows and columns again.

Similarly, at the end of the program, I would like to ask if they would like to restart or not.

  • If they type in Yes, yes, Y, y, I would like it to ask the question about the rows and columns again.

  • If they type in No, no, N, n, I would like it to end the program.

  • If they type something else, be asked to enter Yes/No and have the previous input prompt again

Any help is greatly appreciated!

if __name__ == "__main__":

    o = "Yes"
    while o == "Yes":
        i=0
        x= int(input("Number of columns: "))
        m = x*" _"
        l= "|"+(x*"_|")
        y= int(input("Number of rows : "))
        y=y-1
        print(m)
        for i in range(y+1):
            print(l)
        o=str(input("Would you like to restart? (Yes/No) :"))
        if o == "Yes":
                print("\n" * 49)

        elif o == "":
            print("Please enter Yes or No.")

        else:
            print("See you soon!")

Aucun commentaire:

Enregistrer un commentaire