Python Forum
Issue with logic in homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with logic in homework
#1
Hi,
So my homework is to look at 3 strings. If the string has not before bad I am suppose to replace the everything between not and bad with good.

So the strings are:
This movie is not so bad
This dinner is not that bad!
This tea is not hot!

Should be:
This movie is good
This dinner is good!
This tea is not hot!

Here is my code. I can't figure out how change all three.

def not_bad(s):
    t = s.split()
    a = -1
    b = -1
    newlist = []
    newlist2 = []


    if "bad!" in t:

        for x in t:
            if x == "not":
                a = t.index(x)
            if x == "bad" or "bad!":
                if x == "bad!":
                    yellow = " good!"
                else:
                    yellow =" good"

                b = t.index(x)
        if a < b:
            newlist = range (a, b)

        for z in newlist:
            newlist2.append(z)
        newlist2.append(b)
        newlist2 = list(newlist2)
        newlist2.reverse()

        for take in newlist2:
            del t[take]

        t2 = ' '.join(t)
        t2 = t2 + yellow
        return t2
    else:
        t2 = ' '.join(t)
        return t2



print (not_bad("This dinner is not that bad!"))
print (not_bad("This tea is not hot!"))
print (not_bad("This movie is not so bad"))
I have played around a lot with trying to fix the first loop but no matter what I do either 2 or 3 is always wrong. Trying things like "bad or "bad!" just matches everything because of the article here: https://python-forum.io/Thread-Multiple-...or-keyword

So not sure how to check for both of them. If I could do that then everything would work.
Reply
#2
You should change single character names to meaningful ones
...
hint -- you can ask:
try:
    idx = s.index('bad')
    idx1 = s.index('good')
    if idx < idx1:
       ...
except:
    return s
assuming you can use try/except, you fill in ...
Reply
#3
I also don't understand why if I change it to "bad" and "not" it still changes This tea is not hot. How could it match on those conditions?

(May-24-2018, 12:50 AM)Larz60+ Wrote: You should change single character names to meaningful ones
...
hint -- you can ask:
try:
    idx = s.index('bad')
    idx1 = s.index('good')
    if idx < idx1:
       ...
except:
    return s
assuming you can use try/except, you fill in ...

Ok thank you I will play around with this. I must be missing something about how testing for equivalence works.
Reply
#4
what idx < idx1 is actually saying is, if bad comes before good then do
and if either bad or good is not in the string an exception will be raised.

So you need to check for 1 more 'not' for idx and 'bad' for idx1,
now it says is not before bad!
Reply
#5
(May-24-2018, 02:12 AM)Larz60+ Wrote: what idx < idx1 is actually saying is, if bad comes before good then do
and if either bad or good is not in the string an exception will be raised.

So you need to check for 1 more 'not' for idx and 'bad' for idx1,
now it says is not before bad!

I will study try except blocks. Never used them before. Thank you for putting me back on the path.

Do you know why if I change it to "bad" and "not" it still changes "This tea is not hot"? How could it match on those conditions? I feel like I am not understanding something fundamental.
Reply
#6
first check for not before bad, the logic:
tstring = 'This tea is hot'
try:
    idx = tstring.index('not')
    idx1 = tstring.index('bad')
    if idx < idx1:
        ... change string here
        return changed string
except:
    return unaltered string
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework, works but has logic error goldielocks789 3 1,141 Apr-24-2023, 11:52 AM
Last Post: Larz60+
  saving issue on homework assignment russoj5 2 1,920 Oct-26-2020, 01:53 PM
Last Post: russoj5
  Homework (counting iteration issue) Cardinal07 10 5,590 Feb-26-2018, 04:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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