User Input in R vs Python

April 18, 2012


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”:




Tags: , ,

Comments are closed.