我想在 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)
我还按照这里的建议尝试了 for 循环:
for (i = 0; i < users.length; i++)
无济于事。我会感谢一些帮助。谢谢。