[R] How to make this script ask again

jim holtman
2011-01-01T19:27:58Z

your example works fine for me:

>  repeat {
+ cat("Please, enter a number (Zero ends)")
+ n<-scan(n=1)
+ if(n==0)break
+ i<-("The number is odd")
+ p<-("The number is even")
+ if (n%%2==0)
+ p else i
+ }
Please, enter a number (Zero ends)1: 1
Read 1 item
Please, enter a number (Zero ends)1: 2
Read 1 item
Please, enter a number (Zero ends)1: 3
Read 1 item
Please, enter a number (Zero ends)1: 0
Read 1 item
>

now if you want the answer, you have to use print:

>  repeat {
+ cat("Please, enter a number (Zero ends)")
+ n<-scan(n=1)
+ if(n==0)break
+ i<-("The number is odd")
+ p<-("The number is even")
+ if (n%%2==0)
+ print(p) else print(i)
+ }
Please, enter a number (Zero ends)1: 1
Read 1 item
[1] "The number is odd"
Please, enter a number (Zero ends)1: 2
Read 1 item
[1] "The number is even"
Please, enter a number (Zero ends)1: 3
Read 1 item
[1] "The number is odd"
Please, enter a number (Zero ends)1: 4
Read 1 item
[1] "The number is even"
Please, enter a number (Zero ends)1: 5
Read 1 item
[1] "The number is odd"
Please, enter a number (Zero ends)1: 0
Read 1 item
>


On Sat, Jan 1, 2011 at 12:11 PM, ADias <diasandre at gmail.com>
wrote:
>
> Hi,
>
> as an example I have made this script to give the user the answer if a
> number is odd or even:
>
> ?{
> cat("Please, enter a number (Zero ends)")
> n<-scan(n=1)
> if(n==0)break
> i<-("The number is odd")
> p<-("The number is even")
> if (n%%2==0)
> p else i
> }
>
> If you run this script it will only work once, I mean, after it gives you
> the answer is won't ask for another number. You need to run the script
all
> over again. How could I change it in order to make it ask me for another
> number without having to run the all script again?
>
> I have tried with the "repeat" but it doesn't work
>
> ?repeat {
> cat("Please, enter a number (Zero ends)")
> n<-scan(n=1)
> if(n==0)break
> i<-("The number is odd")
> p<-("The number is even")
> if (n%%2==0)
> p else i
> }
>
> thanks,
>
> Regards,
> ADias
> --
> View this message in context:
http://r.789695.n4.nabble.com/How-to-make-this-script-ask-again-tp3170243p3170243.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?