1

I am trying to get user input with error checking using a while loop to make sure the user always enters something with more than 2 characters. However the Python program never asks me for input. Help?

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)

I'm using Python 2.7 if that makes any difference.

edit: I fixed my code. After the first while loop = need to write True = not False to make it work.

5
  • 10
    True = not True ...are you attempting to rip a hole in spacetime itself?! Commented Feb 1, 2015 at 22:51
  • 1
    should i use True = False instead? Commented Feb 1, 2015 at 22:52
  • 1
    @JamesWat no, you can't change the value of True just like you can't change the value of 1 Commented Feb 1, 2015 at 22:52
  • the first loop work, this is my output: fred jones bob smith mary doe john cardinal frank jesus james watson joe blogs jay harris thank you for entering a long name ['fred jones', 'bob smith', 'mary doe', 'john cardinal', 'frank jesus', 'james watson', 'joe blogs', 'jay harris', 'placeholder_name_for_user_input_via_console'] Commented Feb 1, 2015 at 22:54
  • 3
    @AdamSmith: FWIW in Python 2 you can change the value of True, so the comparison with changing 1 doesn't quite fit. It wasn't until 3 it was promoted from a standard name to a keyword. Commented Feb 1, 2015 at 22:55

2 Answers 2

4

You can't change the value of True. True is the Trueest thing. Instead use 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 exits the innermost loop it's found in.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually in Python2, you can reassign True. There is certainly no reason to ever do it though
@JohnLaRooy-AKAgnibbler what?! Holy crud that sounds like it would break things.
yes - the first loop will exit, but the second will be skipped because it's basically a while False:
-1

I fixed my code. After the first while loop i need to write True = not False to make it work:

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)

1 Comment

The syntax you used does not throw any exception, however the logic of your answer is wrong and rather dangerous. A correct and acceptable answer has already been given to your question, follow it. Gosh, its only good thing is to make one understand better the functioning of booleans in Python 2.x and trigger thoughts about how to fix messed up True-False variables!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.