This code words as intended in Python:
my_books = ['R','Python','SQL','Java','C']
cou = 0
for i in my_books:
cou = cou + 1
print('Book Number:',cou,'&','Name of the book:',i)
print('\nNo more books in the shelf')
Output is:
Book Number: 1 & Name of the book: R
Book Number: 2 & Name of the book: Python
Book Number: 3 & Name of the book: SQL
Book Number: 4 & Name of the book: Java
Book Number: 5 & Name of the book: C
No more books in the shelf
Whereas in R, how to get the same output? My code in R as below:
my_books = c('R','Python','SQL','Java','C')
cou = 0
for(i in my_books){
cou = cou + 1
paste('Book Number:',cou,'&','Name of the book:',i)
}
print('No more books in the shelf')
The output I get is: [1] "No more books in the shelf"
Is there a different function to use within a for loop?
pasteand since it does not automatically print inforloop, you need to explicitly mention that as well. In the function above there is noforloop, hencepasteworks fine.