Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98823 > unrolled thread
| Started by | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| First post | 2015-11-14 09:34 -0800 |
| Last post | 2015-11-17 00:43 +1100 |
| Articles | 10 — 9 participants |
Back to article view | Back to comp.lang.python
A Program that prints the numbers from 1 to 100 Cai Gengyang <gengyangcai@gmail.com> - 2015-11-14 09:34 -0800
Re: A Program that prints the numbers from 1 to 100 Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-14 12:49 -0500
Re: A Program that prints the numbers from 1 to 100 BartC <bc@freeuk.com> - 2015-11-14 18:18 +0000
Re: A Program that prints the numbers from 1 to 100 Ammammata <ammammata@tiscalinet.it> - 2015-11-16 11:12 +0000
Re: A Program that prints the numbers from 1 to 100 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-14 18:25 +0000
Re: A Program that prints the numbers from 1 to 100 Steven D'Aprano <steve@pearwood.info> - 2015-11-15 12:39 +1100
Re: A Program that prints the numbers from 1 to 100 harirammanohar159@gmail.com - 2015-11-16 03:57 -0800
Re: A Program that prints the numbers from 1 to 100 Chris Angelico <rosuav@gmail.com> - 2015-11-16 23:35 +1100
Re: A Program that prints the numbers from 1 to 100 Jussi Piitulainen <harvesting@is.invalid> - 2015-11-16 15:39 +0200
Re: A Program that prints the numbers from 1 to 100 Chris Angelico <rosuav@gmail.com> - 2015-11-17 00:43 +1100
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-14 09:34 -0800 |
| Subject | A Program that prints the numbers from 1 to 100 |
| Message-ID | <ddada022-49a4-418e-85a1-45cd6da015f7@googlegroups.com> |
I want to write a program in Python that does this ---- "Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"." How do I go about doing it ?
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-11-14 12:49 -0500 |
| Message-ID | <mailman.336.1447523394.16136.python-list@python.org> |
| In reply to | #98823 |
On Sat, Nov 14, 2015 at 12:34 PM, Cai Gengyang <gengyangcai@gmail.com> wrote: > I want to write a program in Python that does this ---- > > "Write a program that prints the numbers from 1 to 100. But for multiples > of three print "Fizz" instead of the number and for the multiples of five > print "Buzz". For numbers which are multiples of both three and five print > "FizzBuzz"." > > How do I go about doing it ? > -- > https://mail.python.org/mailman/listinfo/python-list > You could google fizzbuzz and find lots of solutions. But, to give you a hint, use a for loop with range to get the numbers, use if n % 3 and n % 5 to pick out 3 and 5 multiples, print accordingly -- Joel Goldstick http://joelgoldstick.com/stats/birthdays
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-11-14 18:18 +0000 |
| Message-ID | <n27tp6$ec5$1@dont-email.me> |
| In reply to | #98823 |
On 14/11/2015 17:34, Cai Gengyang wrote:
> I want to write a program in Python that does this ----
>
> "Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."
>
> How do I go about doing it ?
Here's one way, although you're probably expected to use a loop:
print (1)
print (2)
print ("Fizz")
print (4)
print ("Buzz")
print ("Fizz")
print (7)
print (8)
print ("Fizz")
print ("Buzz")
print (11)
print ("Fizz")
print (13)
print (14)
print ("FizzBuzz")
print (16)
print (17)
print ("Fizz")
print (19)
print ("Buzz")
print ("Fizz")
print (22)
print (23)
print ("Fizz")
print ("Buzz")
print (26)
print ("Fizz")
print (28)
print (29)
print ("FizzBuzz")
print (31)
print (32)
print ("Fizz")
print (34)
print ("Buzz")
print ("Fizz")
print (37)
print (38)
print ("Fizz")
print ("Buzz")
print (41)
print ("Fizz")
print (43)
print (44)
print ("FizzBuzz")
print (46)
print (47)
print ("Fizz")
print (49)
print ("Buzz")
print ("Fizz")
print (52)
print (53)
print ("Fizz")
print ("Buzz")
print (56)
print ("Fizz")
print (58)
print (59)
print ("FizzBuzz")
print (61)
print (62)
print ("Fizz")
print (64)
print ("Buzz")
print ("Fizz")
print (67)
print (68)
print ("Fizz")
print ("Buzz")
print (71)
print ("Fizz")
print (73)
print (74)
print ("FizzBuzz")
print (76)
print (77)
print ("Fizz")
print (79)
print ("Buzz")
print ("Fizz")
print (82)
print (83)
print ("Fizz")
print ("Buzz")
print (86)
print ("Fizz")
print (88)
print (89)
print ("FizzBuzz")
print (91)
print (92)
print ("Fizz")
print (94)
print ("Buzz")
print ("Fizz")
print (97)
print (98)
print ("Fizz")
print ("Buzz")
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ammammata <ammammata@tiscalinet.it> |
|---|---|
| Date | 2015-11-16 11:12 +0000 |
| Message-ID | <XnsA5547C234722Bammammatatiscalineti@46.4.82.189> |
| In reply to | #98825 |
Il giorno Sat 14 Nov 2015 07:18:18p, *BartC* inviava su comp.lang.python il messaggio news:n27tp6$ec5$1@dont-email.me. Vediamo cosa scrisse: > Here's one way +1 :) -- /-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\ -=- -=- -=- -=- -=- -=- -=- -=- - -=- >>>>> http://www.bb2002.it :) <<<<< ........... [ al lavoro ] ...........
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-11-14 18:25 +0000 |
| Message-ID | <mailman.337.1447525574.16136.python-list@python.org> |
| In reply to | #98823 |
On 14/11/2015 17:34, Cai Gengyang wrote: > I want to write a program in Python that does this ---- > > "Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"." > > How do I go about doing it ? > This homework problem has been asked repeatedly over the years. Just use your favourite search engine and there will be numerous answers. However if you actually want to learn you'll have to try writing your own code at some point, so see https://docs.python.org/3/library/functions.html#func-range and https://docs.python.org/3/library/stdtypes.html#index-14 operation "x % y". -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-11-15 12:39 +1100 |
| Message-ID | <5647e24a$0$1593$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #98823 |
On Sun, 15 Nov 2015 04:34 am, Cai Gengyang wrote:
> I want to write a program in Python that does this ----
>
> "Write a program that prints the numbers from 1 to 100. But for multiples
> of three print "Fizz" instead of the number and for the multiples of five
> print "Buzz". For numbers which are multiples of both three and five print
> "FizzBuzz"."
>
> How do I go about doing it ?
Start by writing a program that prints the numbers from 1 to 100. Hint:
# Program to print the numbers from 1 to 10.
for i in range(1, 11):
print(i)
One you get that far, you will need to decide which numbers are multiples of
3, which are multiples of 5, and which are multiples of both 3 and 5. Hint:
# Multiples of 7.
for i in range(1, 11):
print(i)
if i % 7 == 0:
print("multiple of seven")
If you need more help, show us the code you have written.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | harirammanohar159@gmail.com |
|---|---|
| Date | 2015-11-16 03:57 -0800 |
| Message-ID | <85eea326-406d-4454-8c3a-859f1e464cfc@googlegroups.com> |
| In reply to | #98823 |
On Saturday, 14 November 2015 23:04:40 UTC+5:30, Cai Gengyang wrote:
> I want to write a program in Python that does this ----
>
> "Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."
>
> How do I go about doing it ?
This would help, but try to write on your own to improve, else you cant make this going long....
i = 1
while i <= 100:
if multipleof_3 and multipleof_5:
print("FizzBuzz")
++i
elif multipleof_3:
print("Fizz")
++i
elif multipleof_5:
print("Buzz")
++i
else:
print(number)
++i
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-16 23:35 +1100 |
| Message-ID | <mailman.357.1447677345.16136.python-list@python.org> |
| In reply to | #98873 |
On Mon, Nov 16, 2015 at 10:57 PM, <harirammanohar159@gmail.com> wrote:
> This would help, but try to write on your own to improve, else you cant make this going long....
>
Here's another version, but with a deliberate bug in it. You may use
this code if and only if your comprehension is sufficient to detect
and correct that bug.
next(filter(None,(print(next(filter(None,reversed(x)))) for x in
enumerate(itertools.islice(itertools.cycle(("Fizzbuzz",0,"","Fizz",not
"Fuzz","Buzz","Fizz",(),bytes(),"Fizz","Buzz",{},"Fizz",False,globals().get(math.pi))),1,101)))),None)
Actually... you know what, I think debugging THAT is a malicious prank
to pull on an experienced Python programmer; plus, if you're caught
submitting that for your project, you deserve all the shame you get.
Trust me, this is NOT good code. Here's the corrected version. It
works, but you do not want to use this.
next(filter(None,(print(next(filter(None,reversed(x)))) for x in
itertools.islice(enumerate(itertools.cycle(("Fizzbuzz",0,"","Fizz",not
"Fuzz","Buzz","Fizz",(),bytes(),"Fizz","Buzz",{},"Fizz",False,globals().get(math.pi)))),1,101))),None)
Enjoy!
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <harvesting@is.invalid> |
|---|---|
| Date | 2015-11-16 15:39 +0200 |
| Message-ID | <lf5fv06rrkp.fsf@ling.helsinki.fi> |
| In reply to | #98876 |
Chris Angelico writes:
>
> Here's another version, but with a deliberate bug in it. [- -]
This one recycles fish in a way that looks a bit worrying but it seems
to work. If there's a bug, it's not deliberate. Except stopping at 15 is
deliberate.
from contextlib import contextmanager as fish
from collections import defaultdict as fowl
@fish
def fish(fish):
chip = fowl(str)
chip.update(((fowl(int)[fish],fish),))
yield chip
with fish("Fish") as f, fish("Bush") as b:
print(*("{}{}".format(f[k%3],b[k%5]) or k for k in range(1,16)))
# prints:
1 2 Fish 4 Bush Fish 7 8 Fish Bush 11 Fish 13 14 FishBush
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-17 00:43 +1100 |
| Message-ID | <mailman.366.1447681418.16136.python-list@python.org> |
| In reply to | #98886 |
On Tue, Nov 17, 2015 at 12:39 AM, Jussi Piitulainen <harvesting@is.invalid> wrote: > This one recycles fish in a way that looks a bit worrying but it seems > to work. Recycles fish? http://www.saigan.com/kidscorner/comics/unhfulb.jpg ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web