Python language basics 12: reading user input

Introduction

In the previous post we looked at how to construct a while-loop and what its purpose is. In this post we’ll quickly learn how read a user’s input.

As a reminder I use the free edition of the PyCharm IDE so all the printouts will be from that environment.

Reading user input

We can use the “input” method to read the user’s input. It comes in two versions. The first version simply waits for the user to enter something in the console without a question:

promptLessInput = input()
print('Your promptless input was: ' + promptLessInput)

If you run this code in PyCharm then at first it may not be obvious what’s happening. You’ll need to click in the output window, type something and press Enter:

Promptless input in Python

Another version of input – an overloaded version – accepts a prompt, which can simply be a question. Consider the following example:

promptedInput = input('What have you got to say: ')
print('Your prompted input is: ' + promptedInput)

If you’re new to programming then you probably won’t understand what “overloaded” means but you can safely ignore it for now.

If you run this code then you’ll see the question in the output window. You’ll need to click at a position after the question, type some input and press Enter.

Prompted input in Python

So, it’s that easy to catch the user’s input in a console window in Python.

Read all Python-related posts on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Python language basics 12: reading user input

  1. Brian Dead Rift Webb says:

    Reblogged this on Brian By Experience.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: