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 8542789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:09:01+00:00 2026-06-11T12:09:01+00:00

This is a homework question and it asks Pick a word. Ask the user

  • 0

This is a homework question and it asks “Pick a word. Ask the user to enter a letter. Check how many times the letter is present in your word and output the number on the screen.” So far I have written it as is and it seems to be working fine:

word = str("python")
letters = len(word)
attempt = str(raw_input ("Enter a letter: "))

while attempt in word:
    count = "python".count(attempt)
    if attempt in word:
                    print "This letter is present ",count, "time(s)."
    attempt = str(raw_input ("Enter another letter: "))

while attempt not in word:
    attempt = str(raw_input ("This letter is not present, enter another letter: "))
    count = "python".count(attempt)
    if attempt in word:
             print "This letter is present ",count, "time(s)."

but occasionally if I am inputting letters the program will stop and no longer take any more inputs. What am I doing wrong? I apologize if the code is very sloppy and poorly written as I am new to programming. Thanks.

  • 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-11T12:09:02+00:00Added an answer on June 11, 2026 at 12:09 pm

    I’m not sure if this is what you’re getting at, but your while loops will execute one after the other. They don’t both apply all the time. So the first while means “keep doing this as long as the user enters a letter that is in the word”, and that loop will keep executing as long as the user enters letters in the word. As soon as the user enters one letter that isn’t in the word, the loop will end and things will move on to the second loop. Execution will never go back to the first loop. Likewise, once in the second loop, if you then enter a letter that is in the word, the loop will end. Since that’s the end of the program, the program will end.

    So if you enter a letter that isn’t in the word, then enter a letter that is in the word, the program will end. Like if you first enter “x” and then enter “y”, it will then stop.

    I think what you really want is more like:

    while True:
        attempt = raw_input("Enter a letter:")
        if attempt in word:
            print "That was in the word", word.count(attempt), "times"
        else:
            print "That was not in the word"
    

    Of course, this program will loop infinitely until you close it by pressing Ctrl-Break or the like.

    There are some other issues with your code. You don’t need to wrap “python” in str, since that already is a string. You also don’t need to wrap raw_input in a string, since raw_input already returns a string. You define a variable called letters but never use it.

    Also, you define word = "python" at the beginning, but then sometimes later you use the word variable, while other times you retype the string “python” in your code. It doesn’t matter for this program, but in general it’s a good idea to assing the variable once and use that everywhere; otherwise you’ll have to change it in many places if you decide to use a different word, which increases the likelihood that you’ll forget to change it in one place, and thereby cause a bug.

    Finally, note that in and count operate on substrings, not just single letters. So entering “yth” as as your input will still work and give a count of 1. There’s not necessarily anything wrong with this, but you should be aware that although you’re asking for letters the person can type in anything, and substrings of any length will still be found in the “word”.

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

Sidebar

Related Questions

My professor asks this question for Homework. Consult a Linux Manual or a Microsoft
This is my homework question: Expand on your Circle class by adding a method
I admit this is a homework question that asks if this is possible. I
This is a homework question, but I think there's something missing from it. It
This is a homework question. Question My attempt (the whole file): http://pastebin.com/TS6mByEj If you
I've got this as a homework question and dont know how I should go
I'm trying to solve this problem, its not a homework question, its just code
This is the last question for my Oracle homework for the year and I
So I got this question in data structures class' homework, I'm translating so I
I have a question about my C++ homework. I am just confused about *this.

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.