User Input in R vs Python

April 18, 2012
By

(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 } }

## PYTHON CODE:

num = 0

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

 

To leave a comment for the author, please follow the link and comment on their blog: Abraham Mathew » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



http://www.eoda.de







ODSC

ODSC

CRC R books series











Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)