Conditional - If, Elif, Else

Asked

Viewed 17 times

0

I am a beginner in Python and I have the following script running by Vscode and Pycharm terminal.

from math import pi
import sys

def help():
    print("É necessário informar o valor do Raio.")
    print("Sintaxe: {} <raio>".format(sys.argv[0][2:]))


def circulo(raio):
    return pi * float(raio) ** 2


if __name__ == "__main__":
    if len(sys.argv) < 2:
        help()
    elif not sys.argv[1].isnumeric():
        help()
        print("O raio deve ser um valor numerico.")
    else:
        raio = sys.argv[1]
        area = circulo(raio)
        print("Área do circulo:", area)
    

I did some tests to validate the conditions, being:

  1. (only the file name) all ok returning the help function();
  2. (file name + str) all ok returning the help() function and the conditional "Elif";
  3. (file name + int) all ok returning last "print" with area calculation;

My last test was to pass a float that is where my problem, because it does not perform the calculation of the area, not me of the error message, and does not reach to the "Else", only goes to the "Elif".

É necessário informar o valor do Raio.
Sintaxe: areacirculo14.py <raio>
O raio deve ser um valor numerico.
  • the method isnumeric() check if all characters are numeric, in the case of a floating point, it is not the case because it has a "dot" between the digits

No answers

Browser other questions tagged

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