Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Computing factorials
#1
Question:
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.

my code:

numb = input("Please add a number: ")
def numb_f(x):
    i = 1
    suma = 1
    while i < int(numb):
        suma =  suma * (i+1)
        i = i + 1
        
print(numb_f(numb))
the output is always "None". Don't know why, please advise.
Reply
#2
If you don't specify a return value in your function, it will return None by default.
First you should store the calculated values, and then you can print it.
Reply
#3
The only way for a python function to return a value different from None is to use a return statement.
Reply
#4
numb = input("Please add a number: ")
def numb_f(x):
    i = 1
    suma = 1
    while i < int(numb):
        suma =  suma * (i+1)
        i = i + 1
    return suma
        
print(numb_f(numb))
Now it works fine.

I also decided to add condition that checks whether input is a number:

numb = input("Please add a number: ")
if type(numb) == int:
    def numb_f(x):
        i = 1
        suma = 1
        while i < int(numb):
            suma =  suma * (i+1)
            i = i + 1
        return suma
        
    print(numb_f(numb))
else:
    print("Please add number!")
but this time it prints else statemenet all the time. What did I do wrong?

I made a modification, placed a function out of if/else statemenet but still receive the same outcome.

numb = input("Please add a number: ")
def numb_f(x):
    i = 1
    suma = 1
    while i < int(numb):
        suma =  suma * (i+1)
        i = i + 1
    return suma
if type(numb) == int:       
    print(numb_f(numb))
else:
    print("Please add number!")
Reply
#5
You haven't defined numb as an integer yet. Which begs the question why are you asking for a string instead of an integer to begin with?

numb = int(input("Please add a number: "))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
Thank you. It didn't come to my mind that it's possible to define input as integer.
Reply
#7
It's not a definition, it's a type conversion. Input is always a string. Using the function int, tries to convert the string into a integer. If the content of the string is not a valid integer, you'll get a ValueError.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  computing average in nested loops cap510 5 5,178 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Computing average vestkok 2 2,518 Aug-12-2018, 10:02 AM
Last Post: vestkok
  Factorials Problem OmarSinno 4 4,670 Sep-20-2017, 07:05 AM
Last Post: OmarSinno
  computing the factorial of N foolsgold27 12 8,111 Aug-02-2017, 02:29 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020