How to put more than one parameter in Python?

0

I'm learning Python3 and I'm having trouble making a Python implementation.

For example, in C:

for (fatorial = numero; fatorial >= 1; fatorial--)

I want to put this above implementation in python: I already did 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 did not work. How do I do it?

My code that I was able to make 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 that 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)

    }
}
    
asked by anonymous 23.08.2017 / 01:00

2 answers

3

In python and for and slightly different from other languages, being:

for variavel_qualquer in range(valor_inicial,valor_de_Parada,incremento):
    print("Algo")

With this structure you can do a finite number of repetitions, emulating what you would like to do would be:

for x in range(fatorial,0,-1):
    print("Algo")

The 0 is there because in python does not execute the loop when it reaches the stop number, so to be able to keep the loop in how much and may or equal to 1 and put to be may 0.

Edit1: Converting your pseudo-code to python and using the idea of concatenation and formatting with spaces as you wanted. would be something close to that:

n = int (input("Digite um numero: "))

resultado = 1
texto = ""

lista = range(1,n+1)

for x in lista:
    resultado = x * resultado
    if (x != 1):
        texto = str(x) + " x " + texto
    else:
        texto = str(x) + texto

print (texto + " =", resultado) 
    
23.08.2017 / 01:19
1

Python does not have the traditional for free of C. You need to generate a data range according to your need:

n = 1
numero = 5
for fatorial in range(numero, 1, -1):
    n = n * fatorial
print(n)

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
23.08.2017 / 01:11