Factorial Calculator

What we’ve had to do this week:

creative commons licensed (BY-SA) flickr photo by ▓▒░ TORLEY ░▒▓: http://flickr.com/photos/torley/3505324528creative commons licensed (BY-SA) flickr photo by ▓▒░ TORLEY ░▒▓: http://flickr.com/photos/torley/3505324528

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

Details

Since you are implementing this in Python3, resist the urge to call math.factorial(n). Yes that would solve the problem but what would we do if there was no math.factorial() and we had no internet to find someone’s solution?

There are two basic approaches: a loop with an accumulator of the multiplication and a recursive solution. Choose one and implement that. Once that is done, try the other way. If you used a while loop for the solution with a loop, try structuring this with a for loop (or vice-versa).

How I solved the assignment with my Mac:

Program I used: PyCharmCE

  1. First I had to create a function that receives a non-negative integer and returns the value of that non-negative factorial value.

# This function receives a non-negative integer and returns the value of that non-negative factorial value.

def factorial(n):
counter = n
result = 1

The function is defined by the command def. In order to get the factorial number I used a while loop. This loop is running until the factorial value of n is calculated.

In this loop counter is assigned as n, because we still want to keep the „original n“, which was the integer number that the user inserted. With doing so, the programmer is able to understand, which number was used to calculate the factorial number and he can use this number for another function as well. Another thing that we did was assigning result a 1. Why? Because in that function we are multiplying and 1 is the magic number with which you can multiply anything getting no chances in the result (for example: 1*1 = 1, 2*2 = 2).

After I’ve created those assignments, it was to create the actual loop:

while (counter > 1):
result = result * counter
counter = counter – 1
return result

With return result we ask the computer to output the result, which is the factorial value of n. This result can be used in print in order to show the user the result.

2. Next, it was to ask the user to enter an integer number and to show him the result afterwards:

print(„Enter a number of choice to get the factorial: „)
n = int(input())
print(„The factorial of your chosen number „, n, “ is „, factorial(n), „.“)

3. In the assignment we should as well ask the user if he wants to play again. The question should be answered with yes or no. It was clear that I had to create another while loop for this issue. But for me it was difficult to think about a solution. How could I let the computer run the loop again asking the first question? At the end it wasn’t complicated at all. The only thing I had to do was to put the first question and the result at the beginning, then reassign the input and then define the conditions. That was it!

while True:
print(„Enter a number of choice to get the factorial: „)
n = int(input())
print(„The factorial of your chosen number „, n, “ is „, factorial(n), „.“)

again = input(„Do you want to play again? Please enter ‚yes‘ or ’no‘.“)

if again == „yes“:
print („Fun (: Let’s do this again!“)

else:
print(„Okay, have a nice day.“)
break

 

The whole code looks like this:

fc1

When I’m running this it looks like this:

fcrun

 

After I’ve finished with that code, I’ve tried to replace the while loop with a for-loop. I tried to solve the problem without any webhelp and only with the knowledge I had about the for loop. After a little while of thinking I got it! The rest of the code looks the same and it works perfectly.

 

 

____________________________________________________

What were my lessons learned for this week?

Don’t code when you’re tired. This easiest thing can seem so difficult for you when you are tired. Yesterday, I was trying to solve the assignment for hours and I couldn’t do it. The next day, I got back to the assignment and everything made sense. So instead of wasting your time while being tired, go to sleep and get back to the assignment when you have a clear mind.

Hinterlasse einen Kommentar