Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't compare two strings
#1
Hello, I can't check if this string "operation = str(lst[1])" is equal to a mathematical symbol and that the check is carried out indefinitely:

def solution():
    print("Enter an equation")
    calc = input()
    lst = calc.split(' ')
    global x
    global y
    global operation
    while True:
        try:
            operation = str(lst[1])
            x = float(lst[0])
            y = float(lst[2])
        except ValueError:
            print("Do you even know what numbers are? Stay focused!")
            print("Enter an equation")
            calc = input()
        if operation == '+' or operation == '-' or operation == '*' or operation == '/':
            print(calc)
            break
        else:
            print("Yes ... an interesting math operation.You've slept through all classes, haven't you?")
            print("Enter an equation")
            calc = input()


solution()
Reply
#2
lst comes from input(), so it is already a "str". The splits you do keep it. So lst[1] will always be an str, you don't need to cast it further.

operation = lst[1]
You could just see if it's in a collection of mathematical symbols that you want.

if operation in "+-*/":
    ...
Your "else" section will report that the input doesn't have your correct operation. But it doesn't do the right thing. It asks for input and puts that data in calc. But when the loop is rerun, calc is never referenced. The code only looks at lst

The split code to parse the input into portions has to be inside the loop somewhere, otherwise it is only performed once.
uriel likes this post
Reply
#3
Thank you, man! I find a solution)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,755 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  How to compare strings that have spaces hobbyist 2 1,730 Jan-14-2021, 12:44 PM
Last Post: perfringo
  Finding multiple strings between the two same strings Slither 1 2,511 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,214 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  Compare several strings and output average TotallyCoolGuy 2 2,876 Jan-28-2018, 05:07 PM
Last Post: TotallyCoolGuy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020