Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:31:17+00:00 2026-06-15T16:31:17+00:00

I am having issues trying to get a while loop to validate the users

  • 0

I am having issues trying to get a while loop to validate the users input, and then make sure the user doesn’t repeat values. Shown bellow are the two methods I have tried, but I can’t get my head around how to get them to work.

Method 1

def test():
my_list = ["", "", ""]
for i in range(3):
    while (my_list[i] != "one") and \
          (my_list[i] != "two") and \
          (my_list[i] != "three"):

        while (my_list[i] == my_list[0]) and \
              (my_list[i] == my_list[1]) and \
              (my_list[i] == my_list[2]):

            text = "Enter, one, two or three", i + 1, ":"
            try:
                my_list[i] = input(text)
            except KeyboardInterrupt:
                sys.exit()

print(my_list)

Method 2

def test2():

my_list= ["", "", ""]
while len(my_list)!=len(set(my_list)) == True:
    for c in range(4):
        while (my_list[i] != "one") and \
              (my_list[i] != "two") and \
              (my_list[i] != "three"):
            text = "Enter, one, two or three", c + 1, ":"
            try:
                my_list[c] = input(text)
            except KeyboardInterrupt:
                sys.exit()


print(my_list)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:31:18+00:00Added an answer on June 15, 2026 at 4:31 pm

    Why not simplify it a bit:

    my_list = []
    while len(data) < 3:
        data = input(text) # Valid for Python 3, use raw_input(text) in Python 2
        if data in ("one", "two", "three") and data not in my_list:
            my_list.append(data)
    

    This is a more-or-less direct translation of your requirements:

    1. I have a list
    2. While the list is too small
      1. Get some data
      2. If the data is one of the valid values, and not already in the list
        1. Add it to the list

    Using for x in range(y) in this case just adds complexity, because you don’t actually know how many times you want the loop to run.

    There’s no reason to pre-fill the list with invalid values (my_list = ["", "", ""]), since lists can be resized with append.

    Also, your sys.exit code is unnecessary and probably harmful. Just let the exception propagate and it will crash the program itself (unless you catch it, which you shouldn’t almost never do for a KeyboardInterrupt exception).

    Note: Your first version would work if you combined the two while loops into one, it’s just unnecessarily complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to get started with NDjango but am having issues running a
I'm trying to get the Mailchimp datasource from Springest working, and I'm having issues.
I am trying to get php's namespace technique down but having some issues here:
I'm trying to get started with twitter4r but I'm having some issues: Why I
I am having issues trying to return the closest location to the user via
I am working with a MySQL Database and having some issues while trying to
I am having issues while trying to build up a following query in Wordpress
I am having issues while trying to inject custom permission evaluator in spring security:
I'm trying to get connect to my data base and having issues with it
I'm trying to get started with FMOD but I'm having some issues compiling the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.