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

  • Home
  • SEARCH
  • 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 1079451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:52:19+00:00 2026-05-16T21:52:19+00:00

query level: beginner As part of a learning exercise I have written code that

  • 0

query level: beginner

As part of a learning exercise I have written code that must check if a string (as it is build up through raw_input) matches the beginning of any list item and if it equals any list item.

wordlist = ['hello', 'bye'] 
handlist = [] 
letter = raw_input('enter letter: ') 
handlist.append(letter) 
hand = "".join(handlist) 
for item in wordlist: 
    if item.startswith(hand): 
        while item.startswith(hand): 
            if hand not in wordlist: 
                letter = raw_input('enter letter: ') 
                handlist.append(letter) 
                hand = "".join(handlist) 
            else: break 
        else: break 
print 'you loose' 

This code works but how can my code (and my reasoning/approach) be improved?
I have the feeling that my nesting of IF, WHILE and FOR statements is overkill.

EDIT
Thanks to Dave, I was able to considerably shorten and optimise my code.

wordlist = ['hello','hamburger', 'bye', 'cello']
hand = ''
while any(item.startswith(hand) for item in wordlist):
    if hand not in wordlist:
        hand += raw_input('enter letter: ')
    else: break
print 'you loose' 

I’m surprised my original code worked at all…

  • 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-05-16T21:52:19+00:00Added an answer on May 16, 2026 at 9:52 pm

    Firstly, you don’t need the handlist variable; you can just concatenate the value of raw_input with hand.

    You can save the first raw_input by starting the while loop with hand as an empty string since every string has startswith("") as True.

    Finally, we need work out best way to see if any of the items in wordlist starts with hand. We could use a list comprehension for this:

    [item for item in wordlist if item.startswith(hand)]
    

    and then check the length of the returned list if greater than zero.

    However, even better, python has the any() function which is perfect for this: it returns True if any element of an iterable is True, so we just evaluate startswith() for each member of wordlist.

    Putting this all together we get:

    wordlist = ['hello', 'bye'] 
    hand = ""
    
    while any(item.startswith(hand) for item in wordlist):
        hand += raw_input('enter letter: ')  
    print 'you loose' 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this as my query: public Cursor getQuests(int level) { final String[] columns
Say I have a website that allows anyone to log in through oauth or
I have enabled second level and query caches in my app Looks like when
I have a query where I select animals at a particular level along with
I have the following output in a query. SKILL LEVEL SCORERANGE ----------------------------------------------------------------------------- Stunts LOW
I have a query regarding accessibility of top level class from member inner class.
Does rails have any way to have complex sorting at the database query level?
Level: PHP Learner I'm stuck with a checkbox problem. I have a db that
Query gives me some error: Msg 156, Level 15, State 1, Line 4 Incorrect
When using Hibernate 2nd level cache and query cache and not specifying anything inside

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.