1

I'm having trouble splitting a string, it keeps saying that option_convert and option_convert3 is not equal to dollar and pound

Code:http://pastebin.com/HFSW2BU2

Can't get it work here for some reason.

def option1():
    option_convert = input("""
What curreny would you you like to convert and convert to, (Example: dollar & pound) this would allow you to convert dollars to pounds.
""")
    option_convert2 = option_convert.split('&')[0]
    option_convert3 = option_convert.split('&')[1]
    print (option_convert2)
    print (option_convert3)
    if option_convert2 == "dollar" and option_convert3 == "pound":
        print ("test")
    else:
        print("Something went wrong...")
1

1 Answer 1

8
dollar & pound

See the spaces before and after &. That is the culprit here.

`dollar ` != `dollar`

So, that condition will fail. We can use strip function to remove the the whitespaces (newline, space character, tab, linefeed) like this

option_convert2 = option_convert.split('&')[0].strip()
option_convert3 = option_convert.split('&')[1].strip()

See, you are splitting twice in this program. This can be optimized like this

option_convert2, option_convert3 = map(str.strip, option_convert.split('&'))
Sign up to request clarification or add additional context in comments.

3 Comments

I've been noticing your fine regex, but looking at old answers I see that your even more amazing with python. Lots to learn from your answers, upvoting. :)
@zx81 Thank you :-) I am still learning a lot from Stackoverflow
wow, it's inspiring to hear that even on the mountaintop you are still learning. I'm stoked. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.