python - Why does my code continue? -
this code supposed take user input type of shape. take input of height print shape made of *'s (triangle/square now)
the code works until user types in bad input start, correct function name , tries close it. ideas how fix it?
error:
enter shape draw (q quit): sad unknown shape. please try again enter shape draw (q quit): triangle enter size: 6 * ** *** **** ***** ****** enter shape draw (q quit): q goodbye enter size: #this should not there
the whole code """ shape programme jordan hampson"""
def main(): """main boss function""" shape = input("enter shape draw (q quit): ").lower() if shape == "q": print("goodbye") return else: get_valid_shape(shape) call_shape(shape) main() def call_shape(shape): """calls shape""" size = int(input("enter size: ")) get_valid_size(size) if shape == "square": return print_square(size) elif shape == "triangle": return print_triangle(size) else: return main() def get_valid_size(size): """checks see if size valid""" if size <= 0: print("value must @ least 1") main() def get_valid_shape(shape): """gets valid shape""" shape_1 = shape shapes = ["square", "triangle", "q"] while shape_1 not in shapes: print("unknown shape. please try again") return main() def print_square(size): """prints square input""" _ in range(0, size): print(size * "*") def print_triangle(size): """prints triangle in *'s""" _ in range(1, size +1): print((size -(size -_)) * "*") main()
it's due fonction get_valid_shape(), fonction call fonction main() when enter "sad" start new main() in fonction get_valid_shape() (on main process still @ line get_valid_shape(shape)
). when press "q" quit line , pass call_shape(shape)
prevous shape triangle ask input size.
to avoid suggest code
def main(): shape = "" while shape != "q": """main boss function""" shape = "" #reinitialize shape = input("enter shape draw (q quit): ").lower() if shape == "q": print("goodbye") elif get_valid_shape(shape) : call_shape(shape) else: print("unknown shape. please try again") def call_shape(shape): """calls shape""" size = int(input("enter size: "))#ask 1st time withour print error message while get_valid_size(size): #while size false, ask again size = int(input("enter size: ")) if shape == "square": return print_square(size) elif shape == "triangle": return print_triangle(size) def get_valid_size(size): """checks see if size valid""" if size <= 0: print("value must @ least 1") return true #return true loop @ while else : return false #return false break while def get_valid_shape(shape): """gets valid shape""" shape_1 = shape shapes = ["square", "triangle", "q"] if shape_1 not in shapes: return false else : return true def print_square(size): """prints square input""" _ in range(0, size): print(size * "*") def print_triangle(size): """prints triangle in *'s""" _ in range(1, size +1): print((size -(size -_)) * "*") main()
i suggest module pdb in python. it's debug module useful (you can see how run algorithme step step, go somewhere, function etc ...) link
Comments
Post a Comment