Python 修复精灵中的for循环

Python 修复精灵中的for循环,python,vala,genie,Python,Vala,Genie,我想在精灵中做一个简单的密码检查例程,但是我陷入了一个for循环。下面是我想模仿的python代码: #----------------------------------------------- # password_test.py # example of if/else, lists, assignments,raw_input, # comments and evaluations #---------------------------------------------

我想在精灵中做一个简单的密码检查例程,但是我陷入了一个for循环。下面是我想模仿的python代码:

#-----------------------------------------------
# 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."
以下是我所能得到的:

[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."
[indent=4]
初始化
用户:字符串数组={“Fred”、“John”、“Steve”、“Ann”、“Mary”}
密码:字符串数组={“access”、“dog”、“12345”、“kids”、“qwerty”}
打印“输入用户名”
var usrname=stdin.read_line()
打印“输入密码”
变量pwd=stdin.read_行()
var位置=1
变量i=1
对于i=0到i
但是,我在编译器上遇到了错误:

$ 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)
$valac evenmores.gs

evenmores.gs:15.18-15.18:error:syntax error,预期为'do',但得到了'您应该删除
var i=1
并使用
for i:int=0到(users.length-1)

这里有几点:

  • 当像这样使用精灵
    for
    循环时,它只生成一个数字序列。请注意,要生成递减数字序列,您需要使用
    downto
    而不是
    to
    。下面给出了在数组上迭代的更好方法
  • Genie是强类型和块作用域。当您第一次尝试
    for
    循环时,可能会出现错误
    “名称'i'在'main'的上下文中不存在”
    ,这就是您添加
    var i=1
    的原因。但是,您可以将变量声明为
    for
    循环的一部分,如上所示。一般来说,对于基本类型,例如
    string
    int
    ,我更喜欢将类型显式化,但也可以使用类型推断<代码>对于var i=0到(users.length-1)
也将起作用 要在数组上迭代,最好使用
for-item-in-array
语法。对于您的示例,这看起来像:

[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++
当您运行代码时,您将看到代码存在一个基本问题。我认为更好的解决办法是使用字典:

[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."
要点:

  • 精灵词典需要
    libgee
    才能工作,因此您需要安装Gee及其开发文件。要构建程序,请使用
    valac--pkg gee-0.8 my_example.gs
  • 字典由键和值组成。要测试用户名是否不存在,
    运算符,并在
    关键字中输入
    。还要注意
    .keys
  • 要访问包含键的字典方括号中的值,请使用:
    access[username]

查找< <代码> > < /Cord>语句。该链接是C++代码。您可以找到更多的练习,以及一个电报组。谢谢,我的维基关于西班牙语中的精灵。太好了!现在精灵学习者太少了,也加入精灵小组,我们可以在那里互相帮助,扩大社区。我希望精灵社区会增加。文件很难找到。这就是我创建维基的原因。此外,我还为精灵设计了一个徽标,可以在网站上看到。这个标志代表一个G作为打开C的VALA键。这是一个好主意,我以前也接触过一位设计师(为了一个标志)。。。我想帮助你的网站!我能帮忙吗?
[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."
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++