4 User errors: Everybody makes mistakes

 

In this chapter

  • Erik discovers that users don’t always do what you tell them to
  • Erik learns how to use loops to repeat his question to the user
  • Simon helps Erik make the menus more robust

“Yesterday you did a great job, Erik,” Simon said the next day. “You wrote a very good function, you added default arguments, and you tested it.”

“Yes,” Erik answered. “I think it’s a good program. I want to show it to my friends!”

“Wait, wait,” Simon said. “I don’t think it’s ready to use yet.”

“Why? I think it works perfectly!”

“Oh, really? Let me try.” Simon looked like he had something in mind. He started Erik’s program again, and at the first menu he entered coffee:

Erik's drinks
--------------------------------
1 chocolate
2 coffee
3 decaf
Choose your drink: coffee
 Traceback (most recent call last):                                 #1
  File "/home/erik/mu_code/menu_function.py", line 18, in <module>  #1
    drink = menu("Erik's drinks", drinks, "Choose your drink: ")    #1
  File "/home/erik/mu_code/menu_function.py", line 9, in menu       #1
    answer = choices[int(choice) - 1]                               #1
ValueError: invalid literal for int() with base 10: 'coffee'        #1
>>>
#1 Error messages from Python

“What are you doing?” Erik was enraged. “You should enter only numbers and not words!”

New things you have learned today

Code for this chapter