Skip to content

Latest commit

 

History

History
21 lines (14 loc) · 1.06 KB

human-input.md

File metadata and controls

21 lines (14 loc) · 1.06 KB

Human Input

There will be programs we write that would need human input to function properly. As much as we'd like to think we know everything, we should leave it up to the user as well.

This is quite simple in Python. Let's say we'd like find out the user's name:

name = input('Hello, what is your name?\n')
print('Hi {}! Nice to meet you, hope you like Python!'.format(name))

It's as simple as that! Note the following:

  • User input is saved as a string. If you want to you to change its type you'll have convert it.
  • You don't need to have a question asked to received user input, you can just call input() and the user could still enter data.
  • Python does not put any kind of space when displaying input prompts, which is why we added the newline character ourselves.

Exercises

  1. Create a variable called age and prompt the user for theirs. Convert age into an integer, multiply it by 3 and print the result.
  2. Create a python script called rectangle_perimeter.py, it asks the user for the rectangle's length and width and print the perimeter.