0

I'm new to Python, i wanted to start learning it and here i try to rewrite a little code that i wrote in C# before. The program counts factorial, so if i type in 6 it would be 720. This is the python code:

   import array

def countFactorial(tal):
    summ = 1

    for i in range(len(tal)):
            summ *= i
            i += 1
    print("{} is".format(tal))
    print("{}".format(summ))
    return summ

def main():

    lst = []
    for i in range(5):
        values = input("Type in a value: ")
        lst.append(values)

    for s in lst:
        countFactorial(s)
       # print(s)


main()

If the user would type in for example 6, this is what the program for now will type out: "6 is 0" (5 times).

This is a part of the for loop that i tried to rewrite from C# that doesn't work here. Obviously "tal" is passed on as a parameter here in the C# aswell.

for (int i = 1; i <= tal; i++)
        {
            summ *= i;

        }
        Console.WriteLine(tal + " is " + summ);

What i think i need is a while loop that needs to rund the summ code trough like for example 6 times. I've tried this but cannot work it out.

5
  • In countFactorial you modify i twice, once in for i in range... and once in i += 1, the second modification is lost. Commented Jan 21, 2017 at 14:47
  • @finitegraygreen Well what i tried to create there was as you can se in the c# code(if you know c#). Like a "while loop" where i want "i" to increment untill it equals "tal" Commented Jan 21, 2017 at 14:50
  • You are also using the length of tal as the number (a string), so you are using the number of digits as the loop value. Commented Jan 21, 2017 at 14:52
  • So if i understood right, im using the user input to declare how many times it loops? But it will always loop 5 times anyways? @MartijnPieters Commented Jan 21, 2017 at 14:53
  • @kastravec: you are always asking for 5 numbers. Each of those entries remains a str object, because you never convert the value to an int object. String objects are sequences and have length, the number of characters in the string (so len('5') == 1). Commented Jan 21, 2017 at 14:59

1 Answer 1

1

Ranges without an explicit starting value start at 0, so the first time you multiply 1 by 0. From there on out summ stays 0.

Your C# code on the other hand, starts at 1 and include the end value.

Either start your Python range() objects at 1 too and stop at tal + 1:

summ = 1
for i in range(1, tal + 1):
    summ *= i

or add 1 to i when multiplying:

summ = 1
for i in range(tal):
    summ *= (i + 1)

The i += 1 you added was redundant; i is set to the next value in the range on the next iteration, replacing whatever i was bound to before.

You do need to pass in integers; input() in Python 3 returns a string, so you need to convert it to an integer first:

for i in range(5):
    value = int(input("Type in a value: "))
    lst.append(value)
Sign up to request clarification or add additional context in comments.

2 Comments

Ok now i understand the error, but how would i "make" ranges start at 1, or the "i" start at 1. do i just "i = 1" under the for loop?
Thanks man it worked! You also explained everything well so now i understand it! Thank you yet again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.