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
#π i need some help with this hw (loops)
37 messages Β· Page 1 of 1 (latest)
@willow karma
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.
Closes after a period of inactivity, or when you send !close.
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
!code
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)
put the output in a codeblock too, just without the file type
is your triangle output correct for larger numbers? there is an extra whitespace on the right-side of every row, like the bottom of n=5 is
* * * * *
^
wait let me check the output for higher numbers
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
wait
yeah there seems to be an extra space
'* ' 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
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
Think about modifying the expression which creates the string
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
do you have any limitations, like cannot use .join() or no if statements?
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
You could multiply by one less than the asterisks you need
then the last asterisk is one without the extra space
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
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
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))
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.