-1

まず、すみません。私はプログラミングとAndroidデバイスでのプログラミングの両方に慣れていませんが、何らかの理由でこのコードをコンパイルしてPython For Androidを使用して実行すると. このエラーを返し続けます:

ファイル "/storage/emulated/programming/pass.py"、4 行目、
ユーザー名 = input("Username: ") File""、1 行目、NameError: name 'user' が定義されていません

security = 0

username = ""
while not username:
    username = input("username: ")
password = ""
while not password:
    password = input("password: ")
if username == "user" and password == "pass":
    security = 5
    print (" Hello, security level is:" , security)
else:
    print("invalid login")

これと同じコードを PC で実行しても、このエラーは発生しません。Python For Android と Droidedit を使用して Python コードを書くことに慣れている人はいますか? 利用可能なすべてのモジュールをインストールしました。

4

1 に答える 1

3

Im guessing p4a is not py3.0+ therefore you need raw_input not input

in python2 raw_input is the same as input in python3+

using input in python2 is the same as doing eval(input("Username:")) in python 3

>>> x = raw_input("Enter Equation:")
Enter Equation: 5 + 3
>>> print repr(x)
'5 + 3'
>>> y = input("Enter Equation:") #this is the same as eval(input(msg)) in py3+
Enter Equation: 5 + 3
>>> print repr(y)
`8`

some further info regarding your error specifically. in python2 input attempts to evaluate the user input into python code. you are entering "user" as the username. python then tries to make it into code interpreting it as a variable. if for example you entered 543 for the username it would work fine. also if you entered "user" including the quotes it would work fine as both would evaluate down to python values (the quotes make it a string instead of a variable, and python knows what ints are)

于 2013-10-24T16:53:22.910 に答える