2

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++)

役に立たない。助けていただければ幸いです。ありがとう。

4

2 に答える 2

1

削除var i = 1して使用する必要がありますfor i:int = 0 to (users.length - 1)

ここにはいくつかのポイントがあります。

  • Genieforループをこのように使用すると、数列を生成するだけです。downtoの代わりに使用する必要がある減少する数列を生成することに注意してくださいto。配列を反復処理するより良い方法を以下に示します
  • Genie は強く型付けされ、ブロック スコープです。最初にforループを試したとき、エラーが発生した可能性があります。"The name 'i' does not exist in the context of `main'"これが追加した理由ですvar i = 1forただし、上記のように変数をループの一部として宣言することはできます。一般に、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 ]
于 2015-10-02T16:02:59.600 に答える