Python Forum
What am I doing wrong to not get the answer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What am I doing wrong to not get the answer
#1
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!
Reply
#2
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?
Reply
#3
(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.
Reply
#4
You need to compare the values for equality, surely?
Reply
#5
(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?
Reply
#6
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.
Reply
#7
(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)
Reply
#8
Good, well done!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Answer for newbie, Sqlite3 Froger 2 812 Sep-27-2023, 05:33 PM
Last Post: noisefloor
  Coding answer for newbie cg3 2 1,111 Aug-05-2023, 12:17 PM
Last Post: jefsummers
  Need help with an assignment, not an answer. celtickodiak 4 2,899 Oct-01-2019, 02:04 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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