NameError: name 'Y' is not defined [closed]

-1

So, I'm trying to make a very simple calculator, (one of my first "projects"). In this calculator you must choose an operation. Since each operation has an associated number, and I use if and elif to make the choice of operation. However when choosing the operation using input, at the time of application check the values chosen, says that the variable in this case y has not been defined. Edit: The problem has been solved but now it has learned another one in which after choosing the numbers The program does not make the accounts and simply finishes.

y = input('.:')

n1 = float(input("Primeiro Número "))
n2 = float(input("Segundo Número "))

if y == 1:
    print(n1, "+" ,n2, "=" (n1+n2))

if y == 2:
    print(n1, "-" ,n2, "=" (n1-n2))
elif y == 3:
    print(n1, "x" ,n2, "=" (n1*n2))
if Y == 4:
    print(n1, "/" ,n2, "=" (n1/n2))
calc()
    
asked by anonymous 29.06.2018 / 00:34

1 answer

1

In the last% of% its variable is if uppercase. In Python, there are differences between uppercase and lowercase in variable names. This is why it says Y does not exist - use Y .

Another thing wrong here is that the return of the y is always a string - when you ask for the numbers that will be the operands, you call input on the return of float , and this will convert them to numbers : OK. But when you ask the desired operation, you leave it as it is, and in%% of it below, it compares the value of the input with integers: the result will always be input . You must either convert the typed value to integer by calling if , or compare it with strings, putting the values of False in the "if" in quotation marks. (ex. int )

    
29.06.2018 / 01:09