-1

First of all, sorry. I am new to both programming and programming on my android device, but for some reason when I go to compile this code and run it using Python For Android. It keeps returning this error:

File "/storage/emulated/programming/pass.py", line 4, in
username = input("Username: ") File"", line 1, in NameError: name 'user' is not defined

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

Now, when I run this same bit of code on my PC, I do not get this error. Is anyone here familiar with writing Python code with Python For Android and Droidedit? I have installed all available modules.

4

1 回答 1

3

我猜 p4a 不是 py3.0+ 因此你raw_input不需要input

在 python2 中与在 python3+raw_input中相同input

input在 python2 中使用与eval(input("Username:"))在 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`

关于您的错误的一些进一步信息。在 python2 输入中尝试将用户输入评估为 python 代码。您正在输入“用户”作为用户名。然后 python 尝试将其转换为将其解释为变量的代码。例如,如果您输入 543 作为用户名,它会正常工作。此外,如果您输入“用户”,包括引号,它会正常工作,因为两者都将评估为 python 值(引号使其成为字符串而不是变量,并且 python 知道整数是什么)

于 2013-10-24T16:53:22.910 回答