为什么即使我打破了它,它仍然继续重复真实的时间?

时间:2013-12-27 03:42:04

标签: python-2.7 while-loop username pickle break

...
elif error.lower() == 'create':
    while True:
        try:
            username = raw_input('What would you like your username to be?  ')
            username2 = raw_input('Please enter the same username again: ')
            while not pickle.load(open("%s.p"%username, "rb"))[1]:
                break
                break
            else:
                pickle.load(open("","rb"))
        except IOError:
            print 'The username is not available. Please try a different one.'
    pword = getpass('What would you like your password to be?  ')
    pword2 = getpass('Please enter the same password again: ')
    while pword != pword2:
          print 'The passwords do not match.'
          pword = getpass('What would you like your password to be?  ')
          pword2 = getpass('Please enter the same password again: ')
    money_left = 0
    isguest = False
    print 'Your username is %s, and your password is %s. You have $%d ingame money.' % (username, pword, money_left)
...

当我尝试在我的True中创建帐户时,我确保用户名在注册之前可用。如果用户名不可用,它会工作并再次询问我,但即使它是,它仍然一直在询问。你能救我吗?

1 个答案:

答案 0 :(得分:1)

break语句突破while not...语句,而不是while True循环。我怀疑你打算写:

if not pickle.load(open("%s.p"%username, "rb"))[1]:
    break

break上的文档为here.

我对您的代码进行了一些调整。看看这是否适合你:

import os.path
while True:
    username = raw_input('What would you like your username to be?  ')
    if os.path.exists("%s.p" % username):
        print 'The username "%s" is not available. Please try a different one.' % (username,)
        continue
    username2 = raw_input('Please enter the same username again: ')
    if username == username2:
        break
    else:
        print "The usernames don't match.  Try again."

while True:
    pword = raw_input('What would you like your password to be?  ')
    pword2 = raw_input('Please enter the same password again: ')
    if pword == pword2:
        break
    else:
        print 'The passwords do not match. Try again.'

money_left = 0
isguest = False
print 'Your username is %s, and your password is %s. You have $%d ingame money.' % (username, pword, money_left)