Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix a while loop with multiple conditions returning an error

I have written code requesting the user to either type "2012" or "2013". I then have a while loop check to see if the user types 2012 or 2013.

The request to enter a valid year is supposed to continue until the user enters a valid number. However, I keep getting an error that an unexpected "{" occurs. I cannot find why this is generating such an error in my code.

Here is the code:

yr_function <-function(){
   year_of_data <- as.numeric(readline("Is the year of concern 2012 or 2013? >>> "))
}

year_of_data = 0
While((year_of_data != 2012) | (year_of_data != 2013)){
    yr_function
if(year_of_data != (2012 | 2013),print("You must type either 2012 or 2013")
}

There error generated reads:

Error: unexpected '{' in "While((year_of_data != 2012) | (year_of_data != 2013)){"

Any help would be much appreciated.

like image 689
Jonathan Charlton Avatar asked Dec 08 '22 16:12

Jonathan Charlton


1 Answers

You have the bones of what should be the general idea here, but you've been tripped up in several places by simple mistakes. Here's an annotated version that works as you describe:

yr_function <-function(){
   res <- as.numeric(readline("Is the year of concern 2012 or 2013? >>> "))
   if (is.na(res)){
    res <- -1
   }
   res
}

One of your first problems revolved around scope. You defined year_of_data both inside this function and just before your while loop. But the variable created inside the function will not refer to the one before the while loop. They exist in separate environments. So I modified this function to instead return the value that is input by the user.

You didn't discuss validation much, which is a big topic. I've added a very small bit of validation here. Basically, if the user enters something that can't be reasonably coerced to numeric, then you'll get an NA value, which will be a headache down the road. So I simply check for that and return -1 in that case. This is not a complete solution to the validation problem.

year_of_data <- 0
while((year_of_data != 2012) & (year_of_data != 2013)){
    year_of_data <- yr_function()
    if (!year_of_data %in% c(2012,2013)){
        print("You must type either 2012 or 2013")
    }else{
        break
    }
}

Here's the fixed while loop. First I fixed a few simple syntax errors (it's while not While, etc). Then I assigned the result of yr_function() to year_of_data, so that we can then check the resulting value and act accordingly.

Your attempt at the if condition (year_of_data != (2012 | 2013)) was not syntactically correct, so I switched to checking if the value was contained in the vector c(2012,2013).

Lastly, you had followed the if with a comma and then a print statement. The comma is another syntax error. So I added braces around the print statement, and added an else clause that breaks out of the while loop if the user entered something correct.

More generally, you may want to investigate the function menu for this sort of task. It will probably make the validation issues a bit simpler to deal with.

like image 68
joran Avatar answered Dec 11 '22 07:12

joran