2
Guys I’m learning Python3 and I’m having trouble making an implementation in Python.
For example, in C:
for (fatorial = numero; fatorial >= 1; fatorial--)
I want to put this implementation up in python: I’ve done it like this:
for (fatorial = numero && fatorial >=1 && fatorial-1)
I’ve done it like this:
for (fatorial = numero and fatorial >=1 and fatorial-1)
and so:
for (fatorial = numero; fatorial >=1; fatorial-1)
And it didn’t work. How I do?
My code I got to work:
n = int (input("Digite um numero: ")) resultado = 1 lista = range(1,n+1) for x in lista: resultado = x * resultado print ("! =", n, resultado)
I’ll put my example I did in Portugol Studio:
programa { inteiro numero, fatorial, resultado = 1 cadeia texto = "" //Variavel para salvar a representação final (3x2x1) funcao inicio() { escreva ("Insira um número a ser fatorado: ") leia (numero) para (fatorial = numero; fatorial >= 1; fatorial--) { // Aqui, se for 1 não precisamos concatenar o sinal de multiplicação (x) se(fatorial == 1){ texto = texto + fatorial }senao{ texto = texto + fatorial + "x" } resultado = resultado * fatorial } escreva (numero, "! = ", texto, " = ", resultado) } }
So, Arcashaid. I can do it that way. But I wanted to put the implementation so that it returns like this: <pre> 5 x 4 x 3 x 2 x 1 = 120 </pre> My code looks like this: <pre>n = int (input("Type a number: ")) result = 1 list = range(1,n+1) for x in list: result = x * result print ("! =", n, result)</pre>
– Sandson Costa
Please edit your question and ask what you did, so it’s easier to view and help you.
– user73316
I edited Arcashaid. See if you can understand my doubt now. Thank you.
– Sandson Costa
I hope this is it, in case there is still something missing warn that I modify
– user73316
Perfect, Arcashaid! could you explain to me what that means? str(x)
– Sandson Costa
In python you cannot concatenate an integer with a string, so I used str to convert the integer to a string of it before concatenating, ie in place of integers 5,4,3,2,1 of the example were concatenated strings of them "5","4","3","2","1" It was clear ?
– user73316
Oh yes! I get it. I did some exercises I used this format I just didn’t know what it meant. Thanks for the help.
– Sandson Costa