Python Forum
What am I doing wrong to not get the answer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: What am I doing wrong to not get the answer (/thread-27879.html)



What am I doing wrong to not get the answer - pav1983 - Jun-25-2020

In this challenge you will be given a relation between two numbers, written as a string.

Here are some example inputs:

"2 = 2", "8 < 7", "5 = 13", "15 > 4"
Write a function that determines if the relation is True or False.

Examples
is_it_true("2 = 2") ➞ True

is_it_true("8 < 7") ➞ False

is_it_true("5 = 13") ➞ False

is_it_true("15 > 4") ➞ True
Notes
Tests will only have three types of relations: =, >, and <
Many approaches work here, but the eval() function is particularly useful!


Here is my code. It doesn't work. It passes nearly every test. In the message box, I get this error: ERROR: Traceback:
in <module>
in is_it_true
File "<string>", line 1
2=2
^
SyntaxError: invalid syntax


Here is my code:
def is_it_true(relation):
	if eval(relation) is True:
		return True
	else:
		return False
What am I missing here??? I'm so close!


RE: What am I doing wrong to not get the answer - ndc85430 - Jun-25-2020

Of course it doesn't work for the equals case, because a single = in Python means assignment and you can't assign to a literal, can you?


RE: What am I doing wrong to not get the answer - pav1983 - Jun-25-2020

(Jun-25-2020, 03:25 PM)ndc85430 Wrote: Of course it doesn't work for the equals case, because a single = in Python means assignment and you can't assign to a literal, can you?

An = just sets a variable equal to something and doesn't evaluate. I just don't know how and where to put that in. I don't know where it wants me to go from there.


RE: What am I doing wrong to not get the answer - ndc85430 - Jun-25-2020

You need to compare the values for equality, surely?


RE: What am I doing wrong to not get the answer - pav1983 - Jun-25-2020

(Jun-25-2020, 04:53 PM)ndc85430 Wrote: You need to compare the values for equality, surely?

That's what it says. But the question is 'how'? There's where I'm lost. I don't understand what to do in the context of the problem? Where do I put everything?


RE: What am I doing wrong to not get the answer - ndc85430 - Jun-25-2020

Is there a way you could look at the characters in the string to help you determine what you need to do? Look up the methods that you can use on strings to help you find out what's possible.


RE: What am I doing wrong to not get the answer - pav1983 - Jun-25-2020

(Jun-25-2020, 05:03 PM)ndc85430 Wrote: Is there a way you could look at the characters in the string to help you determine what you need to do? Look up the methods that you can use on strings to help you find out what's possible.

OK. So I need to use another method in addition to eval()? The only one I see is eval() that works in this situation. There has to be another one.

def is_it_true(relation):
	return eval(relation)
This still didn't work but says that most is right. Replace() is another method I could use, but I don't know how to use it in this context.

Nearly fell out of my seat. Kept playing around and didn't give up. It was painful for a while there, even nightmarish. At least I learned something new from it!

def is_it_true(relation):
	chg = relation.replace('=', '==')
	return eval(chg)



RE: What am I doing wrong to not get the answer - ndc85430 - Jun-25-2020

Good, well done!