Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52727 > unrolled thread

Replace blanks with letter

Started byeschneider92@comcast.net
First post2013-08-20 01:35 -0700
Last post2013-08-22 15:44 -0700
Articles 12 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Replace blanks with letter eschneider92@comcast.net - 2013-08-20 01:35 -0700
    Re: Replace blanks with letter Fábio Santos <fabiosantosart@gmail.com> - 2013-08-20 10:16 +0100
    Re: Replace blanks with letter Peter Otten <__peter__@web.de> - 2013-08-20 11:19 +0200
    Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-20 13:45 -0700
      Re: Replace blanks with letter Dave Angel <davea@davea.name> - 2013-08-20 21:54 +0000
    Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-21 00:49 -0700
      Re: Replace blanks with letter Chris Angelico <rosuav@gmail.com> - 2013-08-21 19:24 +1000
      Re: Replace blanks with letter Dave Angel <davea@davea.name> - 2013-08-21 11:42 +0000
      Re: Replace blanks with letter John Gordon <gordon@panix.com> - 2013-08-21 14:35 +0000
    Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-22 15:42 -0700
      Re: Replace blanks with letter Dave Angel <davea@davea.name> - 2013-08-23 01:28 +0000
    Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-22 15:44 -0700

#52727 — Replace blanks with letter

Fromeschneider92@comcast.net
Date2013-08-20 01:35 -0700
SubjectReplace blanks with letter
Message-ID<2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com>
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[i] in input1:
        blanks = blanks[:i] + letters[i] + blanks[i+1:]


What am I doing wrong in this code?

Thanks
Eric

[toc] | [next] | [standalone]


#52731

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-08-20 10:16 +0100
Message-ID<mailman.54.1376990224.19984.python-list@python.org>
In reply to#52727

[Multipart message — attachments visible in raw view] — view raw

On 20 Aug 2013 09:42, <eschneider92@comcast.net> wrote:
>
> 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[i] in input1:
>         blanks = blanks[:i] + letters[i] + 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[i] 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[i] = letters[i]  # or 'letter' if you used enumerate()
...
blanks = ''.join(blankslst)

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

[toc] | [prev] | [next] | [standalone]


#52732

FromPeter Otten <__peter__@web.de>
Date2013-08-20 11:19 +0200
Message-ID<mailman.55.1376990390.19984.python-list@python.org>
In reply to#52727
eschneider92@comcast.net wrote:

> 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[i] in input1:
>         blanks = blanks[:i] + letters[i] + 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))

[toc] | [prev] | [next] | [standalone]


#52745

Fromeschneider92@comcast.net
Date2013-08-20 13:45 -0700
Message-ID<102afb35-1c8d-4957-9e9c-14b17429437b@googlegroups.com>
In reply to#52727
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

[toc] | [prev] | [next] | [standalone]


#52746

FromDave Angel <davea@davea.name>
Date2013-08-20 21:54 +0000
Message-ID<mailman.66.1377035672.19984.python-list@python.org>
In reply to#52745
eschneider92@comcast.net wrote:

> 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)

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#52753

Fromeschneider92@comcast.net
Date2013-08-21 00:49 -0700
Message-ID<89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com>
In reply to#52727
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.

[toc] | [prev] | [next] | [standalone]


#52760

FromChris Angelico <rosuav@gmail.com>
Date2013-08-21 19:24 +1000
Message-ID<mailman.73.1377077079.19984.python-list@python.org>
In reply to#52753
On Wed, Aug 21, 2013 at 5:49 PM,  <eschneider92@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


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

[toc] | [prev] | [next] | [standalone]


#52764

FromDave Angel <davea@davea.name>
Date2013-08-21 11:42 +0000
Message-ID<mailman.79.1377085363.19984.python-list@python.org>
In reply to#52753
eschneider92@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@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")

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#52766

FromJohn Gordon <gordon@panix.com>
Date2013-08-21 14:35 +0000
Message-ID<kv2j7s$35r$1@reader1.panix.com>
In reply to#52753
In <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> eschneider92@comcast.net writes:

> 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.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#52850

Fromeschneider92@comcast.net
Date2013-08-22 15:42 -0700
Message-ID<060f97db-188d-4078-9d87-c70230914ff1@googlegroups.com>
In reply to#52727
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.

[toc] | [prev] | [next] | [standalone]


#52855

FromDave Angel <davea@davea.name>
Date2013-08-23 01:28 +0000
Message-ID<mailman.147.1377221330.19984.python-list@python.org>
In reply to#52850
eschneider92@comcast.net wrote:

> 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.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#52851

Fromeschneider92@comcast.net
Date2013-08-22 15:44 -0700
Message-ID<f883b3ed-a7c3-4c1d-b1e0-e7dd50d3e9c7@googlegroups.com>
In reply to#52727
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 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 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. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web