Delete a variable the program receives in input.split(" ") in Python

Asked

Viewed 65 times

-4

I am working on a calculator that needs to receive an operation (such as SUM, DIV, LOG10 etc.) and operators on the same line, eg: SUM 2 4

But when I get LOG10 or ROOT the user will only type an operator, example: ROOT 36

The code works in the calculating part and when the user type 3 strings in the input, when he type two (as in the case of the root) the program generates error. My difficulty is to have my input delete this extra variable if the user does not type it.

The code is:


operation, n1_str, n2_str = input().split(" ")
n1 = float(n1_str)
n2 = float(n2_str)

while((operation != "RAZ") | (operation != "LOG10")):

    if(operation == "SUM"):
        SUM = n1 + n2
        print("%.2f" %(SUM))
    elif(operation == "DIF"):
        DIF = n1 - n2
        print("%.2f" %(DIF))
    elif(operation == "DIV"):
        DIV = n1 / n2
        print(DIV)
    elif(operation == "MULT"):
        MULT = n1 * n2
        print("%.2f" %(MULT))
    elif(operation == "POT"):
        POT = n1 ** n2
        print("%.2f" %(POT))
    break

while((operation == "RAIZ") | (operation == "LOG10")):
    n1 = int(input())

    if (operation == "RAIZ"):
        RAIZ = n1 ** 0.5
        print("%.2f" %(RAIZ))
    else:
        LOG10 = math.log(n1, 10)
        print("%.2f" %(LOG10))
    break
  • 2

    It’s so much easier to ask for the data in isolation. I don’t know why this craze of people who use Python started wanting to take one data and manipulate it. Recently nobody did this and was happier. So much can go wrong doing so. So either do it another way or accept the mistake and understand that the person should be responsible for typing right, that for an exercise all right, but for something real has to do all treatment, not just partial.

  • Yes, it would be much easier to take alone. But the exercise asks for entry in this way. :/

  • interesting, I didn’t even know that python accepted the operator "or" as |, always used or

  • 1

    Isabella, as it was not mentioned in any of the answers below, I think it important to mention that your use of the while is completely and absolutely unnecessary. Note that no matter what the input(), it will always rotate once. A loop that only rotates once is not a loop. A simple if there would solve

  • 1

    @Yoyo In fact the | is a bitwise operator and only "works" by coincidence, because the boolean in Python is a subclass of int. Remembering that the logical operators like the or and and are not exactly the same, as they have the characteristic of being Circuit short (only evaluates the second condition if necessary), and in Python they return the value of the evaluated expression (different from bitwise, which always returns numbers)

  • 1

    @hkotsubo I found it interesting, I’ll take the cue and link that post, which I found interesting, although not in python, and that. Very instructive! You are exposed to whom you are interested

Show 1 more comment

2 answers

-1

One way to solve the problem would be to take the whole value of the input, divide it into variables (using the split(" ")) and store variables in a list:

list_input = input().split(" ")

Once done, we can already define the value of the variables operation and n1_str, taking into account that these will always be the values stored in the index 0 and 1 of our list:

operation = list_input[0]
n1_str = list_input[1]
n1 = float(n1_str)

Now, to see if there will be a third value just measure the size of the list. If it has size equal to 3, it means that we will need to create the variable n2_str:

if(len(list_input) == 3):
   n2_str = list_input[2] 
   n2 = float(n2_str)

And then you’ll be checking in properly :)

Complete code

import math

list_input = input().split(" ")
operation = list_input[0]
n1_str = list_input[1] 
n1 = float(n1_str)

if(len(list_input) == 3): 
   n2_str = list_input[2] 
   n2 = float(n2_str)

while((operation != "RAZ") | (operation != "LOG10")):

    if(operation == "SUM"):
        SUM = n1 + n2
        print("%.2f" %(SUM))
    elif(operation == "DIF"):
        DIF = n1 - n2
        print("%.2f" %(DIF))
    elif(operation == "DIV"):
        DIV = n1 / n2
        print(DIV)
    elif(operation == "MULT"):
        MULT = n1 * n2
        print("%.2f" %(MULT))
    elif(operation == "POT"):
        POT = n1 ** n2
        print("%.2f" %(POT))
    break

while((operation == "RAZ") | (operation == "LOG10")):

    if (operation == "RAZ"):
        RAIZ = n1 ** 0.5 
        print("%.2f" %(RAIZ))
    else:
        LOG10 = math.log(n1, 10) 
        print("%.2f" %(LOG10))
    break

-2

So, that was my way to fix it:

dados = input().split(" ")
if len(dados) == 3:
  operation, n1, n2 = dados
  n1 = flaot(n1)
  n2 = float(n2)
elif len(dados) == 2:
  operation, n1 = dados
        n1 = float(n1)
operation = operation.upper()

The upper() function on Operation was just to prevent error if the person forgets to write in capital letters, I hope it helped, strong hug

From the code you made, I think you know what the Len() function does, but if you don’t know, Len() analyzes the size of a list or string. In this case, if the list size was 3, it means that the operation, number 1 and number 2 were placed. But if the list size was 2, it means that only the operation and the first number were placed.

Browser other questions tagged

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