尝试为Rstudio创建一个简单的密码保护代码

时间:2014-11-14 12:47:33

标签: r loops if-statement passwords

所以我决定尝试创建一个非常基本的密码保护功能,每次打开Rstudio时都会运行。几个小时后,我想出了以下代码

whoRU <- function()
    {
    password<- "mypassword"
    Henry<- "Henry"
    n <- readline(prompt="Who are you then? ")
    if(n== Henry){
            print(p<-readline(prompt= "If you are Henry, you must know the password? "))
            if(p== password){
                    (print("Welcome home, lets get coding!"))
            }
            else{
                    (print(p2<- readline(prompt="Dude, you suck! Shall we try that one again?")))
                     if( p2== password){
                             (print("Welcome home, lets get coding!"))     
                     }
                     else{
                             (print(p3<- readline(prompt= "Mate you are killing me! I am going to give you one more chance because I like you. What is the password?")))

                     }
                    if(p3== password){
                            (print("Welcome home, lets get coding!"))
                    }
                    else{
                            print("Dude you suck! No coding for you")

                            quit(save = "yes")

                    }

            }


    }
    else{
            print(ps<- readline(prompt= "Hello stranger, you are going to have to provide the password I'm afraid:"))
                    if(ps== password){
                            (print("Welcome home, lets get coding!"))
                    }
                    else{

                            (print(ps2<- readline(prompt="Dude, you suck! Shall we try that one again?")))
                            if( ps2== password){
                                    (print("Welcome home, lets get coding!"))     
                            }
                            else{
                                    (print(ps3<- readline(prompt= "Mate you are killing me! I am going to give you one more chance because I like you. What is the password?")))

                            }
                            if(ps3== password){
                                    (print("Welcome home, lets get coding!"))
                            }
                            else{
                                    print("Dude you suck! No coding for you")

                                    quit(save = "yes")


                            }
                    }



    }

             }

这段代码似乎工作得相当好,但我愿意接受如何改进它的建议。我的主要问题是一些非常轻微的问题。首先,一旦用户输入密码,密码就会打印到控制台。即。

    If you are Henry, you must know the password? mypassword
[1] "mypassword"

我不希望发生这种情况。其次,一旦输入了正确的密码,代码似乎会打印欢迎信息两次,我不希望它这样做。

Who are you then? Henry
If you are Henry, you must know the password? mypassword
[1] "mypassword"
[1] "Welcome home, lets get coding!"
[1] "Welcome home, lets get coding!"

现在我的想法是,它可能正在运行if循环的两次迭代(其中Henry和p ==密码,以及任何其他名称和ps ==密码)但我无法弄清楚如何纠正这个以便它不会发生。

感谢任何帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

上面的@hrbrmstr正确识别了打印问题 - 这里有一些代码问题你应该为可读性而定位(我可以推荐Advanced R Programming Style Guide吗?你不需要围绕print的表达式语法( )电话)

我有点挣扎着看到这段代码的意义;人们可以,嗯,删除那些线。 R是一种固有的开放语言。如果您希望实际提供某种安全性或加密,那么您可能最好看Hadley's package 'secure'