Introduction to Python
  • Introduction
  • Preface
  • Background
  • Installing Python
  • Getting Started
  • Basics
    • More Printing
    • Strings
    • Numbers
    • Exercises
    • Comments
  • Variables
    • Operators
    • Type Conversion
    • Exercises
    • A Brief Introduction to Lists
    • Game Exercise
  • Human Input
  • Functions
    • First Functions
    • Why Functions
    • Exercises
  • Indentation
  • Decisions
    • Booleans
    • Logical Operators
    • If Statements
    • Elif and Else Statements
    • Exercises
    • Rock, Paper, Scissors
    • Game Exercise
    • Game Exercise 2
  • Lists
  • Loops
    • For Loops
    • While Loops
  • More Data Structures
    • Tuples
    • Dictionaries
  • Pygame
  • Extra Content
    • Computers and Code
    • More About Python
    • For Loops with Range
    • List Slicing
Powered by GitBook
On this page

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.

PreviousGame ExerciseNextFunctions

Last updated 6 years ago