重复执行一个函数,直到使用while循环满足r中的条件为止

时间:2018-07-16 21:47:36

标签: r function if-statement while-loop boolean

这可能不是最有效的方法,但是我已经在一本书的帮助下为水果机(可以工作)创建了一个代码(不确定我是否可以共享标题,但是非常适合菜鸟)。但是,这段代码非常简单,它运行一个函数,该函数依次调用其他两个函数:

  1. pull()通过从预定向量中随机选择字符来模拟车轮机制。
  2. prize()分析来自pull()的响应,以使用查找表为符号添加值。

该代码有效,但我想对其进行修改,以使该代码一直运行到到达三颗钻石的累积奖金为止。 "DD"。我还希望它量化所有其他响应,直到达到目标为止,因此我尝试了一个while循环,该循环似乎什么也没产生:

 play2 <- function(){
  response <- pull()

  # I put the while loop straight after calling the first function,
  # hoping that if the condition is not met, then the first function
  # runs again, creating a new "response" until conditions are met.

  while (sum(response == "DD") != 3) {

  # I set the condition so that if the jackpot is not achieved, the 
  # while loop causes the other responses to be investigated. It 
  # it determines this by counting booleans. 

    # The following assignments are designed to begin the count for how
    # many responses that are considered prizes occur, and how often no
    # prize occurs.

    cherry_prize <- 0
    B_prize <- 0
    BB_prize <- 0
    BBB_prize <- 0
    seven_prize <- 0
    no_prize <- 0

    if ("C" %in% response){
      cherry_prize <- cherry_prize + 1



      # A cherry prize occurs if any "C" is returned. The following
      # statements will asses the other non-jackpot three of a kinds.

    }
    else if (sum(response == "B") != 3) {
      B_prize <- B_prize + 1
    }
    else if (sum(response == "BB") != 3) {
      BB_prize <- BB_prize + 1
    }
    else if (sum(response == "BBB") != 3) {
      BBB_prize <- BBB_prize + 1
    }
    else if (sum(response == "7") != 3) {
      seven_prize <- seven_prize + 1
    }
    else {
      no_prize <- no_prize + 1
    }
  }

  # After the jackpot is achieved, the number of other prizes (or lack
  # thereof) that were returned are quantified, prior to the jackpot 
  # were won. The jackpot triplicate would then also be returned and
  # quantified.

  print(cherry_prize)
  print(B_prize)
  print(BB_prize)
  print(BBB_prize)
  print(seven_prize)
  print(no_prize)
  print(response)

  # The last function quantifies the jackpot, once it has been reached.
  prize(response)
}

希望我提供了足够的信息。非常感谢所有帮助,并希望它可以帮助其他人。

1 个答案:

答案 0 :(得分:0)

感谢您的建议。因此,我按照说明进行操作,并遇到警告,该警告阻止该功能运行。

pip3 install telethon-sync

所以我再次认为我使用的编码方法不是最有效的方法,但是我用Error in symbols == "DD" : comparison (1) is possible only for atomic and list types 进行了纠正。

as.list()

这给出了输出:

play2 <- function(){

  runs <- 0
  cherry_prize <- 0
  B_prize <- 0
  BB_prize <- 0
  BBB_prize <- 0
  seven_prize <- 0
  no_prize <- 0

  while (sum(as.list(response) == "DD") != 3) {

    runs <- runs + 1

    response <- pull()

    if ("C" %in% response){
      cherry_prize <- cherry_prize + 1
    }
    else if (sum(as.list(response) == "B") != 3) {
      B_prize <- B_prize + 1
    }
    else if (sum(as.list(response) == "BB") != 3) {
      BB_prize <- BB_prize + 1
    }
    else if (sum(as.list(response) == "BBB") != 3) {
      BBB_prize <- BBB_prize + 1
    }
    else if (sum(as.list(response) == "7") != 3) {
      seven_prize <- seven_prize + 1
    }
    else {
      no_prize <- no_prize + 1
    }
  }
  print(runs)
  print(cherry_prize)
  print(B_prize)
  print(BB_prize)
  print(BBB_prize)
  print(seven_prize)
  print(no_prize)
  print(response)
  score(response)
}

我的代码中未显示的部分内容正在影响我的BBB,7且没有奖金。