当我运行此程序时:
import time
import sys
login = "Mongoose"
password = "secret"
x = 1
print "Welcome User"
print ""
login = raw_input("Username: ")
password = raw_input("Password: ")
if login == 'Mongoose':
x = x + 1
else :
x = x - 1
if password == 'secret':
x = x + 1
else :
x = x - 1
if x > 1:
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = '...\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
time.sleep(0.5)
string = 'Access Granted\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
if x < 1:
string = 'Access Denied\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.10)
它会发出此错误消息:
Traceback (most recent call last):
File "./login3.py", line 27, in <module>
for char in string:
NameError: name 'string' is not defined
有谁知道如何解决这个问题?
答案 0 :(得分:1)
如果x&lt; = 1。
,则尚未定义字符串if x > 1:
string = '...\n'
for char in string: # <<< string is not yet defined if x <= 1.
答案 1 :(得分:1)
if x > 1:
string = '...\n'
此行导致您的错误,您应该使用string
缩进代码的其余部分。或者您应该在if语句中添加else
以将string
设置为默认值。