#πŸ”’ MOOC exercise

62 messages Β· Page 1 of 1 (latest)

static pike
#

Kinda blank on what to do next. Also I've been regularly posting for help for the exercises I'm doing is that bad? I mean the people that help me dont directly give me the answer they guide me and I end up reaching the solution myself is this a bad thing or still learning. I still feel satisfied when i reach the solution but it sucks when I start the exercise and my mind goes blank

Anyways the topic we just learnt is nested while loops (ive been on the same topic for a couple days since i didnt touch the mooc) so im expecting to use it within this exercise

what we've already done is check if the number is less than or equal to 0 completely break the program, what we need to program now is the factorial function basically

errant pulsarBOT
#

@static pike

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.

static pike
#

I jus noticed lol but the first line should be
a while loop

lethal hemlock
#

asking for help definitely isn't a bad thing

#

what you'd want to do next is compute the factorial of the number that the user has inputted. You can do this by iterating through all of the numbers between 1 and num and multiplying them all together

static pike
#

iterating through the numbers would be so much easier with a for loop but theyve only taught me a while loop in this course so far so I dont think they want me to use a for loop

#

should I just use a for loop anyways (ive learnt it before)

lethal hemlock
#

if you wanted to use a while loop you could decrement num by 1 until it reaches 0

#

you'd sort of be doing it backwards, but since the order doesn't matter when multiplying integers it won't make any difference

static pike
lethal hemlock
#

yes, you could do that

static pike
#

im just tryna think of how to actually write the factorial logic but so clueless sry lol πŸ’€

example of how it wants me to print
Please type in a number: 3
The factorial of the number 3 is 6

should I write the print f string there now or later

lethal hemlock
static pike
#

ohh so you'd keep on adding it to the result each time it loops

lethal hemlock
#

it is more like we're multiplying result by it each time it loops

static pike
#

num = int(input("Please type in a number: "))

while num <= 0:
    break

result = 1

while num > 0:
    result *= num
    num -= 1

print(f'The factorial of the number {num} is {result}')
print("Thanks and bye!")

it prints out
Please type in a number: 3
The factorial of the number 0 is 6
Thanks and bye!

#

it prints out num as 0 bcs its 0 after the while loop, I probably put the print at the wrong location

lethal hemlock
#

ah that's because we've modified num in the while loop

#

you could store the initial value in another variable so you can print it out at the end

static pike
#

okay now one thing i didnt realise until now lol is that our program ends once it gets the factorial of one number

#

but its meant to keep going until user inputs something less than or equal to 0

#

so we probably need a loop to keep asking for user input

lethal hemlock
#

yeah, you would have to wrap the whole thing in a while loop

static pike
#

the entire program?

static pike
lethal hemlock
#

well, we want all of the steps to happen for each number

lethal hemlock
#

or...```python
num = # get user input
while num <= 0:
# calculate factorial here
# get user input again

static pike
#

but

static pike
#

did you mean while num >= 0:

#
num = int(input("Please type in a number: "))

temp = num

while num <= 0:
    break

result = 1

while num > 0:
    result *= num
    num -= 1

print(f'The factorial of the number {temp} is {result}')
print("Thanks and bye!")

(just leaving my current code here, this only works for one input)

static pike
lethal hemlock
static pike
#
num = int(input("Please type in a number: "))
temp = num

result = 1

while num > 0:
    result *= num
    num = int(input("Please type in a number: "))
    num -= 1

print(f'The factorial of the number {temp} is {result}')
print("Thanks and bye!")

uhh

#

small problem

lethal hemlock
#

so you should really have two loops here

#

one which loops every time the user wants to calculate the factorial of a new number and an inner one which actually multiplies the numbers together

static pike
#

so something like this?

while num > 0:
    num = int(input("Please type in a number: "))
    while True:
        result *= num
        num -=1
#

or otherway round

lethal hemlock
#

yeah it would be structured like that

static pike
#

where would you put the print statement

lethal hemlock
#

after the inner loop

#

because after that you will have computed the factorial of the number

static pike
#

program infinitely loops after this

#

maybe while True first

lethal hemlock
#

yeah, I think the while True version is a little easier to get your head around

static pike
#

do we still need a nested loop with the while true method

lethal hemlock
static pike
#

ok cool now my program works with the factorial and prints properly but it doesnt end when you input 9

#

0*

#

done

#

all i had to do was add a if statement to break if its <= 0

#

while True:
    num = int(input("Please type in a number: "))
    temp = num

    if num <= 0:
        break

    result = 1

    while num > 0:
        result *= num
        num -= 1
    print(f'The factorial of the number {temp} is {result}')

print("Thanks and bye!")

#

ty! (its time to get stuck on the next exercise KaguyaSmile )

static pike
#

!close

errant pulsarBOT
#
Python help channel closed with !close

This help channel has been closed. 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.