Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38248
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Issue with my code |
| Date | 2013-02-05 19:06 -0500 |
| References | <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1399.1360109194.2939.python-list@python.org> (permalink) |
On 2/5/2013 1:38 PM, maiden129 wrote:
> Hi,
>
> I'm trying to create this program that counts the occurrences
> of each digit in a string which the user have to enter.
>
> Here is my code:
>
> s=input("Enter a string, eg(4856w23874): ")
> s=list(s)
Unnecessary conversion.
> checkS=['0','1','2','3','4','5','6','7','8','9']
checks = '0123456789' is much easier to type.
>
> for i in s:
> if i in checkS:
Loop through checks, not s
> t=s.count(i)
> if t>1:
> for k in range(1,t):
> s=s.remove(i)
> print(i, "occurs", t,"times.")
>
> elif t==1:
> print(i,"occurs 1 time.")
> else: pass
Replace everything with
s = input("Enter a string of digits: ")
for d in '0123456789':
c = s.count(d)
if c:
print("{} occurs {} time{}".format(d, c, '' if c == 1 else 's'))
Enter a string of digits: 12233344499
1 occurs 1 time
2 occurs 2 times
3 occurs 3 times
4 occurs 3 times
9 occurs 2 times
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 10:38 -0800
Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 10:43 -0800
Re: Issue with my code marduk <marduk@python.net> - 2013-02-05 13:56 -0500
Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 11:20 -0800
Re: Issue with my code Dave Angel <davea@davea.name> - 2013-02-05 14:43 -0500
Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 12:19 -0800
Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 12:19 -0800
Re: Issue with my code darnold <darnold992000@yahoo.com> - 2013-02-05 13:37 -0800
Re: Issue with my code marduk <marduk@python.net> - 2013-02-05 17:05 -0500
Re: Issue with my code darnold <darnold992000@yahoo.com> - 2013-02-05 14:25 -0800
Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 11:20 -0800
Re: Issue with my code MRAB <python@mrabarnett.plus.com> - 2013-02-05 19:06 +0000
Re: Issue with my code Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-05 17:20 -0500
Re: Issue with my code Terry Reedy <tjreedy@udel.edu> - 2013-02-05 19:06 -0500
Re: Issue with my code rusi <rustompmody@gmail.com> - 2013-02-05 22:29 -0800
Re: Issue with my code rusi <rustompmody@gmail.com> - 2013-02-05 22:32 -0800
csiph-web