User Input in R vs Python

[This article was first published on Abraham Mathew » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)