-5

This is for an assignment at school and I'm stuck on this question.

Write an algorithm which displays on a new line on the screen, the first 20 values of multiples of 9, using a formula as follows. 9 * n, 9*(n+1),9*(n+2)

I am supposed to make an algorithm with pseudocode or a flow chart, I've been trying it with python3.4 but I cant seem to print out more than (9,18,27).

This is my code:

def multiples():

  for n in range(0, 2):
      a = 9*n, 9*(n+1), 9*(n+2)
  print (a) 

Can someone please tell me where I'm going wrong and possibly show me a better way of doing this with pseudocode or a flow chart. (Thats how the teacher wants it done) I just use Python to see if it actually works. Thank you.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
PhillipK
  • 1
  • 1
  • 3
    Indentation matters in Python - think about what *actually happens* when you step through your code. Also, there's no point both having a `for` loop and hard-coding `n`, `n+1`, etc. – jonrsharpe Oct 22 '15 at 10:49
  • 1
    It looks like you are asked to print 9, 9*2, 9*3... until 9*20 – siritinga Oct 22 '15 at 10:50
  • @Phillipk: can you give some input and output? – Sakib Ahammed Oct 22 '15 at 10:50
  • def multiples(): for n in range(0, 2): a = 9*n, 9*(n+1), 9*(n+2) print (a) multiples() This was my code and the output was: (9, 18, 27) – PhillipK Oct 22 '15 at 10:54
  • The question reads as though you'd need 9*n, 9*(n+1), 9*(n+2) all the way up to 9*(n+19) (first 20 values, but starting at 0, means you finish at 19) So your function would need to take an 'n' to find out where to start. Then the range would need to go from n to n+20, and inside the loop would be just 9*n. – Simon Fraser Oct 22 '15 at 10:55
  • 5
    With "9 * n, 9*(n+1),9*(n+2)," your teacher is trying to demonstrate the pattern your program should produce, not _give you the exact code you need to write_. It's like an English teacher saying "write an essay" and you hand in a piece of paper that says "an essay." – TigerhawkT3 Oct 22 '15 at 10:58
  • I can't believe I missed that! haha. So could a basic pseudocode be: Start print 9*n, 9*(n+1), 9*(n+2),.... 9*(n+20) End – PhillipK Oct 22 '15 at 11:02
  • 1
    That's not pseudocode; that's your assignment. – TigerhawkT3 Oct 22 '15 at 11:04

1 Answers1

0

There you go

def multiples():
   for n in range(0,20):
      a = 9*n
      print (a) 

the n in for n in range(0,2) will vary, so no need to hardcode n+1, n+2

note also, if you're using python 2.7, you probably should use xrange instead of range (in this case it wont matter much, but for big loop it could)

Julien Rousé
  • 1,115
  • 1
  • 15
  • 30
  • A code dump answer with no explanation is about as useful as an assignment dump question with no attempts at a solution. – TigerhawkT3 Oct 22 '15 at 11:56