Print all if conditions

Asked

Viewed 44 times

-3

I wanted a way that when the user typed the key = 4, all the conditions I assigned were printed, have some mode or I would have to assign an if condition and repeat everything again?

import math
import sys

print()
print("#" * 51)
print("Este programa calculará o SENO, COSSENO e TANGENTE.")
print("#" * 51)
print()

lista1 = [1,2,3,4,5]

a = int(input("Para realizar os cálculos digite: \n\nSENO = 1 \nCOSSENO = 2 \nTANGENTE = 3 \nTODOS = 4 \nENCERRAR PROGRAMA = 5 \n\nDigite a seguir: "))
print("#" * 51)
print()
 
while a not in lista1:
        print("Ei, digite um dos números informados!")
        print()
        a = int(input("Para realizar os cálculos digite: \n\nSENO = 1 \nCOSSENO = 2 \nTANGENTE = 3 \nTODOS = 4 \nENCERRAR PROGRAMA = 5 \n\nDigite a seguir: "))
        print("#" * 51)
        print()

if a == 1:
    co = float(input("Digite o valor do CATETO OPOSTO: "))
    hi = float(input("Digite o valor da HIPOTENUSA: "))
    print("#" * 51)
    print()
                  
    sen = co / hi

    print(f"O valor do SENO é igual à {round(sen, 3)}!")
    print()
    
if a == 2:
    ca = float(input("Digite o valor do CATETO ADJACENTE: "))
    hi = float(input("Digite o valor da HIPOTENUSA: "))
    print("#" * 51)
    print()
                  
    cos = ca / hi

    print(f"O valor do COSSENO é igual à {round(cos, 3)}!")
    print()

if a == 3:
    co = float(input("Digite o valor do CATETO OPOSTO: "))
    ca = float(input("Digite o valor do CATETO ADJACENTE: "))
    print("#" * 51)
    print()
                  
    tan = co / ca

    print(f"O valor da TANGENTE é igual à {round(tan, 3)}!")
    print()

if a == 5:   
    print("Encerrando programa...")
    print()
    sys.exit(0)

#Quando o usuario digitasse a key 4 todos os cáculos seriam impressos
  • Separate the specific code portions into function and for each option call the function or related functions(s).

  • It wouldn’t be enough to add one or on your terms? if a == 1 or a == 4, or more simply if a in {1, 4}

  • @Woss thanks friend, helped a lot, solved my problem

  • Ideally you would make a different logic for the same value 4, because if you call again from the previous ifs will ask the user to type values repeatedly.

2 answers

1

It is a simple matter of logic. If you want a code to be running when a = 1 or when a = 4, just make such a condition.

Today you have:

if a == 1:
  ...

Just do:

if a == 1 or a == 4:
  ...

Or, alternatively, you can create a set of possible values and check whether the a is in that set:

# testa se a é 1 ou 4
if a in {1, 4}:
  ...

0

First separate the recurring code into functions.
In programming language the basic idea of function is to encapsulate a code that can be invoked/called by any other part of the program, that is, a function is nothing more than a subroutine to be used in your program.

The use of functions aims at modularizing a system by being able to divide it into several parts in which each part will perform a well-defined task.
In python we define a function using the keyword def.

Since all operations perform the same processing, collect two values and divide them, a function was created calc(opr, l1, l2) which accepts three text parameters where:

  • opr is the text to be displayed stating which operation is performed.
  • l1 is the text to be displayed informing one side of the triangle to be operationalized.
  • l2 is the text to be displayed informing the other side of the triangle to be operationalized.

The functions seno(), cosseno() and tangente() just call out calc() with the appropriate arguments.

Then better simplify the loop where the system processing options will be chosen, removing the verbose code by calling the respective functions for each option.

To simplify the example no input verification and validation code has been added.

def calc(opr, l1, l2):
  a = float(input(f"Digite o valor do {l1}: "))
  b = float(input(f"Digite o valor da {l2}: "))                
  r = a / b
  print(f"\nO valor do {opr} é igual à {round(r, 3)}!\n{'#' * 51}\n")

def seno():
  calc("SENO", "CATETO OPOSTO", "HIPOTENUSA")
  
def cosseno():
  calc("COSSENO", "CATETO ADJACENTE", "HIPOTENUSA")

def tangente():
  calc("TANGENTE", "CATETO OPOSTO", "CATETO ADJACENTE")

print(f"\n{'#' * 51}\nEste programa calculará o SENO, COSSENO e TANGENTE.\n{'#' * 51}\n")    
while True:
  a = int(input("Para realizar os cálculos digite: \n\nSENO = 1 \nCOSSENO = 2 \nTANGENTE = 3 \nTODOS = 4 \nENCERRAR PROGRAMA = 5 \n\nDigite a seguir: "))
  print(f'{"#" * 51}\n')
  if a == 1:
    seno()
  elif a == 2:
    cosseno()
  elif a == 3:
    tangente()
  elif a == 4: #Quando o usuário digitar 4 todos os cálculos serão realizados.
    seno()
    cosseno()
    tangente()
  elif a == 5:   
    print("Encerrando programa...\n")
    break
  else:
    print("Ei, digite um dos números informados!\n")

Test the code on Repl.it

Browser other questions tagged

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