Replace blanks with letter

Dave Angel davea at davea.name
Wed Aug 21 07:42:24 EDT 2013


eschneider92 at comcast.net wrote:

> Thanks. I am running into a bunch of problems with the following code, all of which are clear when running the program
>
> import random
> letters='abcdefg' 
> blanks='_'*len(letters) 
> print('type letters from a to g')
> print(blanks)
> for i in range(len(letters)):
>     if letters[i] in input(): 
>         blanks = blanks[:i] + letters[i] + blanks[i+1:]
>         print(blanks)
>
> If anyone could post an example of how to correctly code this, I would appreciate it. I can't seem to figure it out.
>
> I'll definitely heed Fabio's advice for future reference, but I don't think it's related to the problems I'm currently experiencing. If it is, and I'm just not getting it (most likely the case), please post an example of how to implement his code advice in doing what I wish to accomplish here.

Nowhere have you told us just what the homework assignment was. 
Depending on the goal, this could be "fixed" in various ways.  As it
stands, you are asking the user 7 times to type in the letters from a to
g.  So long as he responds each time the same way, it'll gradually fill
in the letters from left to right, and end up with all seven showing.

In fact, it'll do that even if the user just types the particular single
letter you're asking for.  So in my last response below, I typed a
string that didn't have all 7, but it did have a g, so that was good
enough.

davea at think2:~/temppython$ python3.3 eric.py 
type letters from a to g
_______
abcdefg
a______
abcdefg
ab_____
abcdefg
abc____
abcdefg
abcd___
abcdefg
abcde__
agcdbfe
abcdef_
aggecca
abcdefg
davea at think2:~/temppython$ 

Maybe the problem is that you don't tell the user whether he has
succeeded or not.  To tell that, just stick a test at the end, outside
the for-loop.

if blanks == letters:
    print("Good job")
else:
    print("You lose, run again, and guess what I wanted")

-- 
DaveA





More information about the Python-list mailing list