Creating a program in Python

Asked

Viewed 116 times

-2

Hello, I’m not very good in programming, and I’m having difficulties in this exercise, this giving error already searched and I can not solve... I would like someone to help me to redo this code. I thank anyone who can help me

Create a program that reads a value in real and converts it to dollar and euro. It is necessary to define the value of each currency

valor = float(input('Digite um valor em reais: '))
while opção != 2:
print('''[1] Dolar[2] Euro''')
opção = float(input('Digite sua opção: '))
if opção == 1:
calcular = valor * (3.70)
print('R$ {} é {} em dolares'.format(valor, calcular))
elif opção == 2:
calcular = valor * 4.24print('R$ {} é {} em euros'.format(valor, calcular))
else:
print('Opção invalida')
  • Considering that your code is properly indented, the problem is that you are declared a variable with special characters, such as ç and ã. Always declare variables without accents and without cedilhas, this applies to almost all programming languages.

  • 1

    Juu, the indentation of your code is all wrong in the question. Could you tell us if your code is exactly so or if the indentation got wrong just by posting here?

  • 1

    @user129140 Not for Python, it accepts stress smoothly.

2 answers

2

First let’s analyze the problems. You need to identar your code correctly, is the basis to develop in python!

valor = float(input('Digite um valor em reais: '))

while opção != 2:
    print('''[1] Dolar[2] Euro''')
    opção = float(input('Digite sua opção: '))

    if opção == 1:
        calcular = valor * (3.70)
        print('R$ {} é {} em dolares'.format(valor, calcular))
    elif opção == 2:
        calcular = valor * 4.24
        print('R$ {} é {} em euros'.format(valor, calcular))
    else:
        print('Opção invalida')

Now we can see the following:

# Você está verificando o valor de opcao, antes mesmo de iniciá-la! 
while opção != 2:
    print('''[1] Dolar[2] Euro''')
    opção = float(input('Digite sua opção: '))

When you run this code, you will fall into this error:

Traceback (most recent call last):
  File "so.py", line 3, in <module>
    while opção != 2:
NameError: name 'opção' is not defined

To solve this, you need to initialize this variable with a value!

In doing so, your program will execute correctly ;) Note that I changed the variable name option by option, since it is not a good practice in any programming language to use variables with Ç, accents, etc.

valor = float(input('Digite um valor em reais: '))
opcao = 0

while opcao != 2:
    print('''[1] Dolar[2] Euro''')
    opcao = float(input('Digite sua opção: '))

    if opcao == 1:
        calcular = valor * (3.70)
        print('R$ {} é {} em dolares'.format(valor, calcular))
    elif opcao == 2:
        calcular = valor * 4.24
        print('R$ {} é {} em euros'.format(valor, calcular))
    else:
        print('Opção invalida')

I took the liberty, and added a little validation to the selected option, and added a third option, which is to quit the program

valor = float(input('Digite um valor em reais: '))
opcao = 0

while opcao != 3:
    print('''
          1 - Dolar
          2 - Euro
          3 - Sair
          ''')

    entrada = input('Digite sua opção: ')

    if entrada:
        try:
            opcao = int(entrada)
        except:
            print('Insira uma das opções informadas')
            continue

    if opcao == 1:
        calcular = valor * (3.70)
        print('R$ {} é {} em dolares'.format(valor, calcular))

    elif opcao == 2:
        calcular = valor * 4.24
        print('R$ {} é {} em euros'.format(valor, calcular))

    elif opcao == 3:
        print('Até mais ;)')

    else:
        print('Opção invalida')

Any questions, just ask.

Big hug ;)

  • 2

    Could explain why "it is not a good practice in any programming language to use variables with Ç, accents"?

  • Pq languages typically use only characters contained in the ascii table, as well as the English alphabet. If you test your code in python 2, you will see q it will error, simply because a string does not recognize Unicode

-1

You’re using accents in variables. Do not start variables with uppercase letters or special characters if you have to use 2 words in a variable use nome_grande or nomeGrande and never use accents.

Utilize \n to jump line:

print('--------\n[1] Dolar\n[2] Euro\n[0] Sair\n--------')

Your while is: as long as it is different from 2 continue. I created a variable called question and assign it the value True of the kind Boolean, while the value is True your While will be running.

I added:

elif opcao == 0:
    # opcap for 0(ZERO) pergunta = False, While vai parar
    pergunta = False

When option is equal to zero:

The variable pergunta will receive the value of False and so will stop your While.

# inicie o While como True
pergunta = True
valor = float(input('Digite um valor em reais: '))
#  Enquanto pergunta for True(Verdadeira) o While vai funcionar
while pergunta:
    print('--------\n[1] Dolar\n[2] Euro\n[0] Sair\n--------')
    opcao = float(input('Digite sua opção: '))
    if opcao == 1:
        calcular = valor * (3.70)
        print('R$ {} é {} em dolares'.format(valor, calcular))
    elif opcao == 2:
        calcular = valor * 4.24
        print('R$ {} é {} em euros'.format(valor, calcular))
    elif opcao == 0:
        # Opção for 0(ZERO) opcao = False, While vai parar
        pergunta = False
    else:
        print('Opção invalida')
  • 3

    The first paragraph of your answer makes no sense at all. Python accepts without problems accentuation in object names, both with and functions.

Browser other questions tagged

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