Quantcast
Viewing all articles
Browse latest Browse all 14069

How can i error handle user input and a whitespace?

when user input a D for example, i want it to be the same when he enters (D ), if he puts an extra space i mean

import time

title = "\t \t Welcome to the shapes program!"underline = "\t " +"=" * len(title) # Calculates how many letters title has and prints specifically

print("\n", title, "\n", underline)

print("\n \nHere is your menu for today!, input whatever shape you want in the category below!")

while True:print("\nT - Trapezoid \nP - Parallelogram \nR - Rectangle")print("D - Diamond\nY - Triangle\nH - Heart")

shape_1 = input('\nWhat shape do you wish to choose? (T, P, R, D, H or Y only), or type "exit" to leave: ')if not shape_1.strip():  # Check if input is empty or only contains spaces    print("\n \t\t\tyou entered nothing, please enter T, P, R, D, H or Y only")    continueif shape_1.lower() == "exit":    print("Exiting...")    time.sleep(2)  # Waits for 2 seconds before print what's under this    print("Done!")    breakif shape_1.isdigit():    print("\n \t\t\t please enter a valid letter not a number")    continuetry:    if shape_1.lower() == "t":        print("\n   You chose a trapezoid!\n")        time.sleep(0.3)        print("\t  APEZO  ")        time.sleep(0.3)        print("\t R     I")        time.sleep(0.3)        print("\tTRAPEZOID")        time.sleep(0.3)    elif shape_1.lower() == "p":        print("\n   You chose a parallelogram!\n")        print("\t PARALLELOG")        time.sleep(0.3)        print("\t  A\t   R")        time.sleep(0.3)        print("\t   R\t    A")        time.sleep(0.3)        print("\t    ALLELOGRAM")    elif shape_1.lower() == "r":        print("\n   You chose Rectangle!\n")        print("\tRECTANGLE")        time.sleep(0.3)        print("\tE\tL\n\tC\tG\n\tT\tN\n\tA\tA\n\tN\tT\n\tG\tC\n\tL\tE")        time.sleep(0.3)        print("\tELGNATCER")    elif shape_1.lower() == "d":        print("\n   You chose a diamond!\n")        time.sleep(0.3)        print("\t   D   ")        time.sleep(0.3)        print("\t  III   ")        time.sleep(0.3)        print("\t AAAAA")        time.sleep(0.3)        print("\tMMMMMMM")        time.sleep(0.3)        print("\t OOOOO ")        time.sleep(0.3)        print("\t  NNN  ")        time.sleep(0.3)        print("\t   D   ")    elif shape_1.lower() == "y":        print ("\n You chose a triangle!\n")        print("\t        T")        time.sleep(0.3)        print("\t       R R")        time.sleep(0.3)        print("\t      I   I")        time.sleep(0.3)        print("\t     A     A")        time.sleep(0.3)        print("\t    N       N")        time.sleep(0.3)        print("\t   G         G")        time.sleep(0.3)        print("\t  L           L")        time.sleep(0.3)        print("\t E             E")        time.sleep(0.3)        print("\tTRIANGLETRIANGLE!")    elif shape_1.lower() == "h":        print("\nYou chose a heart!\n")        print("\t   H     E")        time.sleep(0.3)        print("\t A    R    T")        time.sleep(0.3)        print("\tT           T")        time.sleep(0.3)        print("\t R         R")        time.sleep(0.3)        print("\t  A       A")        time.sleep(0.3)        print("\t    E   E")        time.sleep(0.3)        print("\t      H")    else:        print("\n\t\t\t   please enter T, P, R, Y, H or D")except : ("bla bla bla")

if not shape_1.strip(): i thought this would solve it, it only works if user input nothing, or whitespace only


Viewing all articles
Browse latest Browse all 14069

Trending Articles