How to put more than one parameter in Python?

Asked

Viewed 597 times

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)

    }
}

2 answers

5


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")

0 is there because the for is in python does not run the loop when it reaches the stop number, so to be able to keep the loop on how much and May equal to 1 and placed to be May that 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:

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) 
  • 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>

  • Please edit your question and ask what you did, so it’s easier to view and help you.

  • I edited Arcashaid. See if you can understand my doubt now. Thank you.

  • I hope this is it, in case there is still something missing warn that I modify

  • Perfect, Arcashaid! could you explain to me what that means? str(x)

  • 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 ?

  • 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.

Show 2 more comments

2

  • So bigown. I get that way there. But I wanted to put the implementation so that it returns like this: <pre> 5 x 4 x 3 x.

  • There has nothing to do with the question, that’s detail of what to put inside the for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.