I am trying to do some basic challenges

+1 Simon Piras · October 8, 2014
Hello Python friends!

I am trying to practice and review the basis of python. I am using Python 3.4.

I want to write a program that can compute factorial. I tried various ways (with a function or in the following fashion) but I either fail or get an infinite loop.

Would someone be kind enough to give me pointers (but not the whole answer please!)

print("Hello user, give me a value!")
x = int(input())
if x == 0:
    print("1")
elif (x < 0):
    print("No that's impossible")
else:
    while x>0:
        (x*(x-1))
        print(x)

OK...I have not been able to resist and I looked at the solution:

def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)
x = input(int())
print(x)

Do you know any other valid way?

What challenges/exercises/advices would you give to anyone willing to learn more and practice?





Post a Reply

Replies

Oldest  Newest  Rating
0 Simon Piras · October 8, 2014
Thanks!
+1 Diego Mariani · October 8, 2014

print("Hello user, give me a value!")
x = int(input())
f = x
if x == 0:
    print("1")
elif (x < 0):
    print("No that's impossible")
else:
    while x > 1:
        f*=(x-1)
        x = x - 1

print(f)

It's very important that you leave tabs after if statements and also decremeting x
  • 1

Python

107,255 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator