首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复Genie中的for循环

修复Genie中的for循环
EN

Stack Overflow用户
提问于 2015-10-02 12:37:53
回答 2查看 236关注 0票数 2

我想做一个简单的密码检查例程在Genie,但我被困在一个for循环。下面是我想模仿的python代码:

代码语言:javascript
复制
#-----------------------------------------------
# password_test.py
#    example of if/else, lists, assignments,raw_input,
#    comments and evaluations
#-----------------------------------------------
# Assign the users and passwords
users = ['Fred','John','Steve','Ann','Mary']
passwords = ['access','dog','12345','kids','qwerty']
#-----------------------------------------------
# Get username and password
usrname = raw_input('Enter your username => ')
pwd = raw_input('Enter your password => ')
#-----------------------------------------------
# Check to see if user is in the list
if usrname in users:
    position = users.index(usrname) #Get the position in the list of the users
    if pwd == passwords[position]: #Find the password at position
        print 'Hi there, %s. Access granted.' % usrname
    else:
        print 'Password incorrect. Access denied.'
else:
    print "Sorry...I don't recognize you. Access denied."

这是我所能得到的:

代码语言:javascript
复制
[indent=4]

init
    users: array of string = {"Fred","John","Steve","Ann","Mary"}
    passwords: array of string = {"access","dog","12345","kids","qwerty"}

    print "Enter user name"
    var usrname = stdin.read_line()
    print "Enter password"
    var pwd = stdin.read_line()

    var position = 1
    var i = 1
    for i=0 to i < users.length
        if (users[i]==usrname)
            position += 1
            if pwd == passwords[position]
                print "Hi there, %d. Access granted."
            else
                print "Password incorrect. Access denied."
        else
            print "Sorry...I don't recognize you. Access denied."

但是,我在编译器上得到了错误:

代码语言:javascript
复制
$ valac evenmores.gs 
evenmores.gs:15.18-15.18: error: syntax error, expected `do' but got `<' with previous identifier
    for i=0 to i < users.length
                 ^
Compilation failed: 1 error(s), 0 warning(s)

我还尝试了这里中建议的for循环

代码语言:javascript
复制
for (i = 0; i < users.length; i++)

但没有结果。我会接受一些帮助。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-02 16:02:59

您应该删除var i = 1并使用for i:int = 0 to (users.length - 1)

这里有几点:

  • 当Genie for循环像这样使用时,它只是生成一个数字序列。注意,要生成一个递减的数字序列,您需要使用downto而不是to。下面给出了对数组进行迭代的更好方法
  • 精灵是强类型和块范围。当您第一次尝试for循环时,您可能得到了错误"The name 'i' does not exist in the context of `main'",这就是添加var i = 1的原因。但是,您可以将变量声明为for循环的一部分,如上面所示。通常,对于基本类型(如stringint ),我更喜欢将类型显式化,但您也可以使用类型推断。for var i = 0 to (users.length -1)也能工作

要迭代数组,最好使用for item in array语法。对于您的示例,这个示例如下所示:

代码语言:javascript
复制
[indent=4]
init
    users: array of string = {"Fred","John","Steve","Ann","Mary"}
    passwords: array of string = {"access","dog","12345","kids","qwerty"}

    print "Enter user name"
    usrname:string = stdin.read_line()
    print "Enter password"
    pwd:string = stdin.read_line()

    position:int = 0
    for var user in users
        if (user==usrname)
            if pwd == passwords[position]
                print "Hi there, %s. Access granted.", usrname
            else
                print "Password incorrect. Access denied."
        else
            print "Sorry...I don't recognize you. Access denied."
        position++

在运行代码时,您将看到代码存在一个基本问题。我认为更好的解决办法是使用字典:

代码语言:javascript
复制
[indent=4]
init
    var access = new dict of string,string
    access[ "Fred" ] = "access"
    access[ "John" ] = "dog"
    access[ "Steve" ] = "12345"
    access[ "Ann" ] = "kids"
    access[ "Mary" ] = "qwerty"

    print "Enter user name"
    username:string = stdin.read_line()
    print "Enter password"
    pwd:string = stdin.read_line()

    if !(username in access.keys)
        print "Sorry...I don't recognize you. Access denied."
    else
        if pwd == access[ username ]      
            print "Hi there, %s. Access granted.", username
        else
            print "Password incorrect. Access denied."

要点:

  • Genie字典需要libgee才能工作,所以您需要安装Gee及其开发文件。要构建程序,请使用valac --pkg gee-0.8 my_example.gs
  • 字典是由键和值组成的。若要测试用户名不存在,请使用!操作符和in关键字。还请注意.keys
  • 要访问包含键的字典方括号中的值,请使用:access[ username ]
票数 1
EN

Stack Overflow用户

发布于 2016-12-15 21:50:56

代码语言:javascript
复制
init
    users: array of string = {"Fred","John","Steve","Ann","Mary"}
    passwords: array of string = {"access","dog","12345","kids","qwerty"}

    print "Enter user name"
    usrname:string = stdin.read_line()
    print "Enter password"
    pwd:string = stdin.read_line()  
    
    error:int = 0                   
    cont:int = 0        
    for var user in users
        if (user!=usrname)
            error++
            if error == (users.length)      
                print "No reconocido. Acceso denegado."             
        if (user==usrname)
            position:int = cont                 
            if pwd == passwords[position]               
                print "OK: Acceso Concedido."
            else                
                print "Password incorrecta."            
        cont++
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32907351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档