Python while 循环不循环

Python While loop not looping

我正在尝试通过使用 while 循环进行错误检查来获取用户输入,以确保用户总是输入超过 2 个字符的内容。但是 Python 程序从不要求我输入。帮忙?

first_names = ['fred', 'bob', 'mary', 'john', 'frank', 'james', 'joe', 'jay']
last_names = ['jones', 'smith', 'doe', 'cardinal', 'jesus', 'watson', 'blogs', 'harris']
full_names = ['empty_name']*len(first_names)
i = 0
while True:
    full_names[i] = (first_names[i] + " " + last_names[i])
    print full_names[i]
    i = i + 1
    if i == (len(last_names) + len(first_names))/ 2:
        True = not True
name = 'placeholder_name_for_user_input_via_console'
while True:
    name = raw_input("please enter a new name")
    if len(name) < 2 :
        print " please enter a name longer than 2 characters"
    else:
        True = not True
print "thank you for entering a long name"
full_names.append(name)
print(full_names)

我正在使用 Python 2.7,如果有任何不同的话。

编辑: 我修复了我的代码。在第一个 while 循环之后 = 需要编写 True = not False 才能使其工作。

您无法更改 True 的值。 TrueTrue 最好的东西。而是使用 break.

while True:
    full_names[i] = (first_names[i] + " " + last_names[i])
    print full_names[i]
    i = i + 1
    if i == (len(last_names) + len(first_names))/ 2:
        # True = not True; some_int / 0
        break

break 退出它所在的最内层循环。

我修正了我的代码。在第一个 while 循环之后,我需要编写 True = not False 使其工作:

first_names = ['fred', 'bob', 'mary', 'john', 'frank', 'james', 'joe', 'jay']
last_names = ['jones', 'smith', 'doe', 'cardinal', 'jesus', 'watson', 'blogs', 'harris']
full_names = ['empty_name']*len(first_names)
i = 0
while True:
    full_names[i] = (first_names[i] + " " + last_names[i])
    print full_names[i]
    i = i + 1
    if i == (len(last_names) + len(first_names))/ 2:
        True = not True
name = 'placeholder_name_for_user_input_via_console'
True = not False
while True:
    name = raw_input("please enter a new name")
    if len(name) < 2 :
        print " please enter a name longer than 2 characters"
    else:
        True = not True
print "thank you for entering a long name"
full_names.append(name)
print(full_names)