Error in notamf(mf) : p object not found

Asked

Viewed 129 times

0

The code below is for my first college program. What do I call the given of one function within another??? I need these 3 functions for the delivery of the program, but error appears

""Error in notamf(mf) : p object not found""

Obs.: When I enter -1 in some proof, P1 or P2, the result to be considered is the substitute proof grade...

Obs2.: there is a comment on the last line, because the student’s situation is not fulfilled after calculating mf (final average)


n <- as.numeric(readline("Digite o numero de alunos na sala: "))

for (i in 1:n) {
  nome <- readline("Digite o nome do aluno: ")

    p1<- as.numeric(readline("Digite a nota da P1: "))
    p2<- as.numeric(readline("Digite a nota da P2: "))
    psub<- as.numeric(readline("Digite a nota da PSub: "))
    ep1<- as.numeric(readline("Digite a nota do EP1: "))
    ep2<- as.numeric(readline("Digite a nota do EP2: "))
    ep3<- as.numeric(readline("Digite a nota do EP3: "))

notap <- function(p){
  if(p1== -1){
    p<-as.numeric((p2+psub)/2)
}else if(p2== -1){
    p<-as.numeric((p1+psub)/2)
}else{
  p <-as.numeric((p1+p2)/2)
}
return(p)
}

notaep <- function(ep){
  ep <- (ep1 + 2*ep2 + 2*ep3)/5
  return(ep)
}


notamf <- function(mf){

  if(p>=5 && ep>=5){
    mf <- (3*p+ep)/4


  }else{
    if(p>ep){
      mf <- ep
    }else{
      mf <- p
    }

  }
  return(mf)
}

print(paste0("nome = ", nome))
print(paste0("P1= ",p1))
print(paste0("P2= ",p2))
print(paste0("PSub= ",psub))
print(paste0("A média das provas P= " , notap(p)))
print(paste0("A média dos EPs, EP= ", notaep(ep)))
print(paste0("A média final, MF= ", notamf(mf)))
#print(paste0("A situação do aluno ", nome , " é ", situacao))
}

1 answer

0

The following works and is simpler. Here are some of the errors in the original code.

  1. Never define functions within the cycle for.
  2. The functions computed values from what the user entered but these user values were not arguments of the functions. Was defining functions with output values as arguments (input data).

Corrected code.

notap <- function(p1, p2, psub){
  if(p1 == -1){
    p <- (p2 + psub)/2
  }else if(p2 == -1){
    p <- (p1 + psub)/2
  }else{
    p <- (p1 + p2)/2
  }
  p
}

notaep <- function(ep1, ep2, ep3){
  ep <- (ep1 + 2*ep2 + 2*ep3)/5
  ep
}

notamf <- function(p, ep){
  if(p >= 5 && ep >= 5){
    mf <- (3*p + ep)/4
  }else{
    mf <- max(p, ep)
  }
  mf
}

lerTeclado <- function(texto){
  tcl <- readline(texto)
  as.numeric(tcl)
}

n <- lerTeclado("Digite o numero de alunos na sala: ")

for (i in 1:n) {
  nome <- readline("Digite o nome do aluno: ")

  p1 <- lerTeclado("Digite a nota da P1: ")
  p2 <- lerTeclado("Digite a nota da P2: ")
  psub <- lerTeclado("Digite a nota da PSub: ")
  ep1 <- lerTeclado("Digite a nota do EP1: ")
  ep2 <- lerTeclado("Digite a nota do EP2: ")
  ep3 <- lerTeclado("Digite a nota do EP3: ")

  p <- notap(p1, p2, psub)
  ep <- notaep(ep1, ep2, ep3)
  mf <- notamf(p, ep)

  cat("nome =", nome, "\n")
  cat("P1 =", p1, "\n")
  cat("P2 =", p2, "\n")
  cat("PSub =", psub , "\n")
  cat("A média das provas P =" , p, "\n")
  cat("A média dos EPs, EP =", ep, "\n")
  cat("A média final, MF =", mf, "\n")
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.