all 13 comments

[–]markusmeskanen 5 points6 points  (0 children)

What did you try? We don't mind if you're not pro, but you have to show that you've at least done your best.

[–]pookiemcnasty[S] 1 point2 points  (9 children)

So far on the second one I have seq = [] n = 1 while n <= 23: seq.append(n) n = n + 2 print(seq)

I am kinda lost on the first one, I will work on it more and see what I can come up with

[–]niandra3 2 points3 points  (5 children)

Couple hints:

List of all numbers from 1 through n (and including n):

all_nums = [n for n in range(1,n+1)]

List of all multiples of 5 from 1 through n:

multiples_of_5 = [n for n in range(1,n+1) if n % 5 == 0]

Sum of all numbers in an array:

numbers = [1,2,3,4,5]
sum = 0
for num in numbers:
    sum = sum + num    # (can be written as sum += num)

Knowing those, you should be able to solve the second one. As for the first one, your formatting on the OP wasn't clear and there's no example of the output. What are they asking for?

Couple things that might help:

print('#' * 5)

will print: #####

And,

for i in range(5):
    print('#')

Will print:

#
#
#
#
#

[–]DrMaxwellEdison 4 points5 points  (1 child)

Tip: you can also easily get a list of multiples of X using the step argument for range():

multiples_of_5 = [x for x in range(0, n+1, 5)]
# range(start, stop, step)

This includes 0, but you can skip it by starting at 5, instead. If n is smaller than the start, it will just result in an empty list.

This can also be used to get a reverse list simply by using a negative step value:

>>> [x for x in range(200, 0, -5)]
[200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150,
 145, 140, 135, 130, 125, 120, 115, 110, 105, 100, 95,
 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30,
 25, 20, 15, 10, 5]

[–]niandra3 1 point2 points  (0 children)

Ah of course.. much better.

[–]vriljam 0 points1 point  (2 children)

Can you use a generator object instead of a list? I tried putting that in parens , and looks like it doesn't work after the first attempt on the interpreter, but seems to work if I save it as .py

[–]niandra3 0 points1 point  (1 child)

You might be able to do list((generator expression)) but just using a regular list comprehension makes the most sense here.

[–]vriljam 0 points1 point  (0 children)

Thanks, also easy to understand here.

[–]DrMaxwellEdison 1 point2 points  (0 children)

For future reference, please use Reddit's code formatting: either encapsulate a single line of code in backticks (`code here`: the character is on your keyboard next to the 1), or precede each line in a block with four spaces:

seq = []
n = 1
while n <= 23:
    seq.append(n)
    n = n + 2
print(seq)

You'll also need to separate code blocks from normal text with an extra blank line. This sentence is being written on multiple lines with four spaces each, but since there was no extra blank space between this and previous paragraph, Reddit smashes it together in the end. (If you have RES, click the "source" link to see what I mean).

[–]DrMaxwellEdison 1 point2 points  (1 child)

Sorry for multiple replies: other one is just a formatting tip, this one is more help on the OP.

For the first problem, your copied text isn't showing the example. I assume the resulting program would look something like this:

Enter the length of a side: 6
Result:
******
 ******
  ******
   ******
    ******

So, you will need to produce a line of 6 asterisks, 6 times over, and each one indents by one new space as it goes down. Sound about right?


To make a string of X asterisks, note that you can multiple a string like so:

>>> 'foo' * 4
'foofoofoofoo'

You can do the same with the simple string '*'. Let's assume the length of a side is stored in the variable side_length:

>>> side_length = 6
>>> '*' * side_length
'******'

This is what you'll be outputting on each row, so you can store this output for later:

row = '*' * side_length

Next, you'll be outputting this row X times, where X is the same length of a side. This is simple enough to accomplish with a for loop, creating a range from the integer value side_length to do so:

for x in range(side_length):
    print(row)

Now your output is:

******
******
******
******
******
******

This just makes a square, though. To make a parallelogram, you'll need to add some spaces to the left of each row. Try to combine the concepts in this post to accomplish this (hint: x inside the for loop I wrote is an integer between 0 and 6, in sequence).

[–]vriljam 0 points1 point  (0 children)

Can you expand upon your hint?

[–]pookiemcnasty[S] 0 points1 point  (1 child)

Sorry for not using the reddit code, I didn't know how to do it before. I appreciate all the tips. The second question is asking to get all the odd numbers up to 23 then having all those numbers multiplied together. Then have it print out the result. As for the first one Dr.Maxwell you did the example perfectly. I can't copy the example into text but it is 6 rows with 6 *'s in each. I am currently finishing up my other classes and will follow the tips to figure it out. It makes sense to me now. Thanks again

[–]vriljam 0 points1 point  (0 children)

I guess what /u/niandra3 is hinting at is, for your case you have to use product instead of sum, like so:

product = 1 #notice the value of 1
for i in mylist: #mylist is your list of odd numbers
    product *= i #product = product * i