samedi 3 novembre 2018

When running code in R was given an error that there was a missing value where true/false needed and I can't fix it

I am new to using R and have minimal amount of Python experience. I am sure this is an easy fix but I am just not seeing it. I was given a code to run a Fibonacci sequence to 100 and I copy and pasted it, but I am getting the following error code: Error in if (numterms <= 0) { : missing value where TRUE/FALSE needed. I know this has to do with the if/else clause but I am not seeing the problem. I have run through the code a couple different ways but it has not helped. And the person to assist is not available during the weekend. Any help would be appreciated.

# take the max number input from the user
numterms = as.integer(readline(prompt="What is your max number?  "))

# first two items
num1 = 0
num2 = 1
counter = 2

# check if the number of terms is valid
if(numterms <= 0) {
    print("Please enter an  integer above zero")
} else {
    if(numterms == 1) {
        print("The Fibonacci  sequence:")
        print(num1)
    } else {
        print("The Fibonacci  sequence:")
        print(num1)
        print(num2)
        while(counter <  numterms) {
            numth = num1 + num2
            print(numth)
            # update values
            num1 = num2
            num2 = numth
            counter = counter + 1
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire