GithubHelp home page GithubHelp logo

dsc-user-input-and-output-in-python-onl01-dtsc-pt-041320's Introduction

User Input and Output in Python

Introduction

Sometimes, you'll want Python to ask users for a certain input. This is where user inputs can help!

Objectives

You will be able to:

  • Incorporate input/output functionality in code to allow for user interaction

User input in Python

To get user input in Python, you can use the input() function. You can store the result in a variable, and use it to your heart’s content. Remember that the result you get from the user will be a string, even if they enter a number.

For example, run the following cell:

name = input("Give me your name: ")
print("Your name is " + name)
Give me your name: Billy
Your name is Billy

When you use the input() function, the program waits for the user to type something and press ENTER. Only after the user presses ENTER does the program continue.

Manipulating strings (a few ways)

What you get from the input() function is always a string. What can you do with it?

First, make the string into a number. Let’s say you are 100% positive that the user entered a number. You can turn the string into an integer with the function int(). (Later we will see what to do when the user does NOT enter a number and you try to do this. For now, don’t worry about that problem)

Here is what this looks like:

age = input("Enter your age: ")
age = int(age)
print ("You are", age , "years old" )
Enter your age: 43
You are 43 years old

Unlike using + for string concatenation as seen earlier, we simply use a , for joining strings with numbers. You can also convert an int to str and use concatenation normally:

print ("You are " + str(age) + " years old")
You are 43 years old

Example

Let's create a program that asks the user to enter their name, age, and the current year. Print out a message addressed to them that tells them the year that they will turn 100 years old.

name = input("What is your name: ")
age = int(input("How old are you: "))
current_year = int(input("What is the current year: "))
year = str((current_year - age)+100)
print(name + " will be 100 years old in the year " + year)
What is your name: Billy
How old are you: 43
what is the current year: 2019
Billy will be 100 years old in the year 2076

Summary

This lesson introduced you to character input/output and string manipulation. We also saw how we can take user input, do some basic processing and provide feedback to the user based on the input.

dsc-user-input-and-output-in-python-onl01-dtsc-pt-041320's People

Contributors

shakeelraja avatar sumedh10 avatar loredirick avatar mas16 avatar mike-kane avatar lmcm18 avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.