User Input in R vs Python

April 18, 2012

(This article was first published on Abraham Mathew » R, and kindly contributed to R-bloggers)

Both R and Python have facilities where the coder can write a script which requests a user to input some information. In Python 2.6, the main function for this task is raw_input (in Python 3.0, it’s input()). In R, there are a series of functions that can be used to request an input from the user, including readline(), cat(), and scan(). However, I find the readline() function to be the optimal function for this task. In the following blocks of code, I show how to perform the same task using both R and Python.

## R CODE:

num = 0

while(num < 3 ){
num = num + 1
name <- readline(“Hey dude, what’s your name = “)
if( name == “quit” ) { break }
if( name == “Bieber” ) {
cat( “Welcome “, name )
break } }


num = 0

while num < 3:
num = num + 1
name = raw_input(“Hey dude, what’s your name = “)
if name == “Bieber”:
print “Welcome “, name
if name == “quit”:


To leave a comment for the author, please follow the link and comment on their blog: Abraham Mathew » R. offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.