#πŸ”’ i need some help with this hw (loops)

37 messages Β· Page 1 of 1 (latest)

willow karma
#

hello i need some help with this hw that our proffesor gave us, it's supposed to be exercise that we do after a lab explanation but i kinda already solved it, the issue is that it's evaluated by another program in the submission site so the output has to match 100% for it to accept it, i'm just here asking for help on matching the output

dull marshBOT
#

@willow karma

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

willow karma
#

n=int(input())
for i in range(1,n+1):
print(' '(n-i)+' '*i)

#

this is the first code

#

it's supposed to make triangle using *

#

the triangles base and side are supposed to be equal and the triangle should face to the left

meager gull
#

!code

dull marshBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

willow karma
#

now here it fails on test 1 where the expect output is '*'

#

and the one i get is this '* '

#

i tried playing around with it but it would change the shape of the whole triangle, so i can't find a correct edit that matches the output of n=1 and still keeps the shape of the triangle

#

this is n=7 for refrence too:

            *
          * *
        * * *
      * * * *
    * * * * *
  * * * * * *
* * * * * * *
#

oops

#

it seems like i can't send it on here without it getting messed up

#
n=int(input())
for i in range(1,n+1):
    print('  '*(n-i)+'* '*i)
meager gull
lament crystal
willow karma
#

wait let me check the output for higher numbers

#
          * * 
        * * * 
      * * * * 
    * * * * * 
  * * * * * * 
* * * * * * * 
#

wait

#

yeah there seems to be an extra space

lament crystal
#

'* ' this will always leave that trailing whitespace on the right side, try to think about how to add in the necessary number of whitespaces without using that

willow karma
#

i did do a code before where it printed both rows and columns based on a fixed x and y, i used the format function and an if statement where everything is printed with end=' ' until the last one which forces the print function to go to the next line, but idk if that'll really work here

meager gull
#

Think about modifying the expression which creates the string

willow karma
#

i just tried to add a space before the i instead of the string now:

expected output:'  *'
my output:' *'
#

so i just need to add one more soace somehow

#

oh nvm, looking at the logs that actually did nothing

lament crystal
#

do you have any limitations, like cannot use .join() or no if statements?

willow karma
#

we haven't learned join yet but the if statement is an intresting idea

#

looking at it now i could probably make two cases:
one in which it prints normally
and one at the end to prevent the extra whitespace

meager gull
#

You could multiply by one less than the asterisks you need

#

then the last asterisk is one without the extra space

lament crystal
#

you have 2 main cases, n=1 where you don't want any spaces and n>1 where you want spaces between *, see if you can figure out what to do

willow karma
#

i have a class rn so i'll try to do something later about it but after a bit of observation it seems like the program i'm trying to replicate adds multiples of 2s as whitespaces depending on the position of the asterisk based on n:

for example n=3
xxxx*
xx**
***

so for n=2 it's supposed to have 2 whitespaces before the asterisk

lament crystal
#

ok, this is my solution, so try to work on it yourself and if you get stuck, you can take some inspiration, but still try to solve it yourself, otherwise the next task will be harder
||```py
n=int(input())
for i in range(1,n+1):
print(" "(n-i), end="")
print("
", end="")
print(" "(i-1))

dull marshBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.