How to make this program repeat itself?

Asked

Viewed 531 times

0

Which command is useful for running the program from the beginning to the end??

f=float(input("Digite 0 para retânglo, 1 para triângulo:"))

if f==0:
    a=float(input("valor da altura:"))
    b=float(input("valor da base:"))
    print(a*b/2)
else:
    a=float(input("valor da altura:"))
    b=float(input("valor da base:"))
    print(a*b)
  • You want the show to run forever, that’s it?

  • I think you still haven’t seen the language at all, while maybe it’s what you want

1 answer

0


Use the repeat block while. The block’s syntax is while bool:, where, it will repeat the code while the condition is True. Soon your program can stay that way:

stop = False

while not stop:

    f=float(input("Digite 0 para retângulo, 1 para triângulo:"))

    if f==0:
        a=float(input("valor da altura:"))
        b=float(input("valor da base:"))
        print(a*b/2)
    else:
        a=float(input("valor da altura:"))
        b=float(input("valor da base:"))
        print(a*b)

    if input("Deseja sair (Y/N) ? ").lower() == "y":
        stop = True

As the value of the variable initially stop is False and I declared a not, she will have the value True in the syntax of while. If the user type "y" or "Y", the variable stop will have the value True, and in the syntax of whilehe will have the value False. That way he gets off the block while and close the program if there is no more code below.

Browser other questions tagged

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