Replace blanks with letter

E

eschneider92

I'm trying to replace the blank(_) with the letter typed in by the user, in the appropriate blank(_) spot where the letter should be (where is in the letters list).

letters='abcdefg'
blanks='_ '*len(letters)
print('type letter from a to g')
print(blanks)
input1=input()
for i in range(len(letters)):
if letters in input1:
blanks = blanks[:i] + letters + blanks[i+1:]


What am I doing wrong in this code?

Thanks
Eric
 
F

Fábio Santos

I'm trying to replace the blank(_) with the letter typed in by the user,
in the appropriate blank(_) spot where the letter should be (where is in
the letters list).
letters='abcdefg'
blanks='_ '*len(letters)
print('type letter from a to g')
print(blanks)
input1=input()
for i in range(len(letters)):
if letters in input1:
blanks = blanks[:i] + letters + blanks[i+1:]


What am I doing wrong in this code?

Thanks
Eric


First, don't use range(len(iterable)). It's bad practise, and you will have
to use iterable all the time. Try

for i, letter in enumerate(letters):

If you are modifying a string in-place, you could change it into a list,
then back.

blankslst = list(blanks)
....
blankslst = letters # or 'letter' if you used enumerate()
....
blanks = ''.join(blankslst)

Now, why are you not printing the `blanks` string again?
 
P

Peter Otten

I'm trying to replace the blank(_) with the letter typed in by the user,
in the appropriate blank(_) spot where the letter should be (where is in
the letters list).

letters='abcdefg'
blanks='_ '*len(letters)
print('type letter from a to g')
print(blanks)
input1=input()
for i in range(len(letters)):
if letters in input1:
blanks = blanks[:i] + letters + blanks[i+1:]


What am I doing wrong in this code?


`blanks` has two chars per letter in `letters`. If you change the initial
value to

blanks = "_" * len(letters)

your code should work. If you think the output looks better with spaces you
have to adjust the indices -- or you add spaces for printing only with

print(" ".join(blanks))
 
E

eschneider92

Is there also a way to have the code remember what I typed and not stop after the first letter the user types? For example, if I typed 'a' once, thus returning 'a______', and then typed in 'b', I want the code to return 'ab_____' and so on. I wasn't clear about this part in my original post. Thanks for the help.

Eric
 
D

Dave Angel

Is there also a way to have the code remember what I typed and not stop after the first letter the user types? For example, if I typed 'a' once, thus returning 'a______', and then typed in 'b', I want the code to return 'ab_____' and so on. I wasn't clear about this part in my original post. Thanks for the help.

First you need some kind of loop. There's only one call to input() in
your present code, so the question is meaningless.

Roughly speaking, make sure the following line is NOT in your loop:

blanks='_ '*len(letters)
 
E

eschneider92

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 in input():
blanks = blanks[:i] + letters + 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 thinkit's related to the problems I'm currently experiencing. If it is, and I'mjust 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.
 
C

Chris Angelico

Thanks. I am running into a bunch of problems with the following code, all of which are clear when running the program


Some of us don't have time to just execute arbitrary code in some safe
environment, so we'd REALLY rather you paste in the exception
traceback (if it's throwing one), or explain what it ought to be doing
that it isn't doing, or in whatever other way show what the problems
actually are. Remember, what's obvious to you isn't obvious to us;
what you see as an obvious problem might actually be correct
behaviour, so without knowing your expectations, we can't pinpoint the
trouble.

ChrisA
 
D

Dave Angel

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 in input():
blanks = blanks[:i] + letters + 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@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@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")
 
J

John Gordon

In said:
Thanks. I am running into a bunch of problems with the following code, all
of which are clear when running the program

No, they're not clear. We can see what the code does, obviously, but we
don't know what it's *supposed* to do.
 
E

eschneider92

I wanted the program to stop only after all the letters were typed; why in the world would I try to write a program with blanks for each letter that seem intended to be filled, only to have it stop if the last letter is typed, or have to type each letter so many times until its processed? If you ranthe code, my problems, as well as the intended goal, should become obvious.. Also, why wouldn't someone who's willing to help me not run the code (takes a few seconds btw) I'm having trouble with in order to diagnose its faults, yet you have the time to write how you won't run it? If I did have a teacher to help me, this would be the last place I'd come to for help. It should be easy to deduce what I intended this program to do. Please no one respond being as I am done here, I just had to vent, but instead report it if you want.
 
E

eschneider92

I wanted the program to stop only after all the letters were typed; why in the world would I try to write a program with blanks for each letter that seem intended to be filled, only to have it stop if the last letter is typed, or have to type each letter so many times until its processed? If you ranthe code, my problems, as well as the intended goal, should become obvious.. Also, why would someone who's willing to help me not run the code (takes a few seconds btw) I'm having trouble with in order to diagnose its faults,yet you have the time to write how you won't run it? If I did have a teacher to help me, this would be the last place I'd come to for help. It shouldbe easy to deduce what I intended this program to do. Please no one respond being as I am done here, I just had to vent, but instead report it if youwant.
 
D

Dave Angel

I wanted the program to stop only after all the letters were typed; why in the world would I try to write a program with blanks for each letter that seem intended to be filled, only to have it stop if the last letter is typed, or have to type each letter so many times until its processed? If you ran the code, my problems, as well as the intended goal, should become obvious. Also, why wouldn't someone who's willing to help me not run the code (takes a few seconds btw) I'm having trouble with in order to diagnose its faults, yet you have the time to write how you won't run it? If I did have a teacher to help me, this would be the last place I'd come to for help. It should be easy to deduce what I intended this program to do. Please no one respond being as I am done here, I just had to vent,
but instead report it if you want.

It would have been much fewer words to describe the goal of the program.

I ran the code, and showed the results of running it. But I still have
no spec for what your assignment was.

Like maybe the user is supposed to type one character at each prompt,
even though it tells him to type 7. And the program should refuse
any attempt to type more than one letter. And the user can type the
characters in any order, not just A to G, and it'll keep prompting him
till all 7 of the original letters is found. And it'll score him based
on how many tries before he finishes.

Or about 40 other possibilities.


If you do have a teacher, have him tell you about comments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top