please read code better understanding of question. i'm creating list in python. in while loop there's try , except, want set user input type string. , if user types in integer want print out message in "except" block. doesn't execute valueerror if type in integer when run code.
here's code:
to_do_list = [] print(""" hello! welcome notes app. type 'show' show list far type 'done' when you'v finished list """) #let user show list def show_list(): print("here list far: {}. continue adding below!".format(", ".join(to_do_list))) #append new items list def add_to_list(user_input): to_do_list.append(user_input) print("added {} list. {} items far".format(user_input.upper(), len(to_do_list))) #display list def display_list(): print("here's list: {}".format(to_do_list)) print("enter items list below") while true: #here's problem is! #check if input valid try: user_input = str(input(">")) except valueerror: print("strings only!") else: #if user wants show list if user_input == "show": show_list() continue #if user wants end list elif user_input == "done": new_input = input("are sure want quit? y/n ") if new_input == "y": break else: continue #append items list add_to_list(user_input) display_list()
input
returns string. see the docs input
function. casting result of function string won't anything.
you use isdecimal
check if string numeric.
if user_input.isdecimal(): print("strings only!")
this fit in nicely existing else
clause.
Comments
Post a Comment