Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52764
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re: Replace blanks with letter |
| Date | 2013-08-21 11:42 +0000 |
| References | <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.79.1377085363.19984.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web