修复 Genie 中的 for 循环
Fixing a for loop in Genie
我想在 Genie 中制作一个简单的密码检查例程,但是我陷入了 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."
但是,我在编译器上遇到错误:
$ 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)
我还按照 here:
中的建议尝试了 for 循环
for (i = 0; i < users.length; i++)
没有用。我会很感激一些帮助。谢谢。
您应该删除 var i = 1
并使用 for i:int = 0 to (users.length - 1)
这里有几点:
- 当像这样使用 Genie
for
循环时,它只会生成一个数字序列。请注意,要生成递减数列,您需要使用 downto
而不是 to
。下面给出了一种更好的遍历数组的方法
- Genie 是强类型和块范围的。当您第一次尝试
for
循环时,您可能遇到错误 "The name 'i' does not exist in the context of `main'"
,这就是您添加 var i = 1
的原因。但是,您可以将变量声明为 for
循环的一部分,如上所示。通常对于 string
和 int
等基本类型,我更喜欢将类型显式化,但您也可以使用类型推断。 for var i = 0 to (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."
要点:
- Genie 词典需要
libgee
才能运行,因此您需要安装 Gee 及其开发文件。要构建程序,请使用 valac --pkg gee-0.8 my_example.gs
- 字典由键和值组成。要测试用户名是否存在,使用
!
运算符和 in
关键字。另请注意 .keys
- 要访问包含键的字典中的值,使用方括号:
access[ username ]
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++
我想在 Genie 中制作一个简单的密码检查例程,但是我陷入了 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."
但是,我在编译器上遇到错误:
$ 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)
我还按照 here:
中的建议尝试了 for 循环for (i = 0; i < users.length; i++)
没有用。我会很感激一些帮助。谢谢。
您应该删除 var i = 1
并使用 for i:int = 0 to (users.length - 1)
这里有几点:
- 当像这样使用 Genie
for
循环时,它只会生成一个数字序列。请注意,要生成递减数列,您需要使用downto
而不是to
。下面给出了一种更好的遍历数组的方法 - Genie 是强类型和块范围的。当您第一次尝试
for
循环时,您可能遇到错误"The name 'i' does not exist in the context of `main'"
,这就是您添加var i = 1
的原因。但是,您可以将变量声明为for
循环的一部分,如上所示。通常对于string
和int
等基本类型,我更喜欢将类型显式化,但您也可以使用类型推断。for var i = 0 to (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."
要点:
- Genie 词典需要
libgee
才能运行,因此您需要安装 Gee 及其开发文件。要构建程序,请使用valac --pkg gee-0.8 my_example.gs
- 字典由键和值组成。要测试用户名是否存在,使用
!
运算符和in
关键字。另请注意.keys
- 要访问包含键的字典中的值,使用方括号:
access[ username ]
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++