Are there any bugs in doing this? LPTHW Question

Put your programming skills to the test in these challenges.

Are there any bugs in doing this? LPTHW Question

Post by ghostheadx2 on Tue Mar 31, 2015 4:04 pm
([msg=87512]see Are there any bugs in doing this? LPTHW Question[/msg])

Code: Select all
  if "0" in next or "1" in next:
    how_much=int(next)
  else:
    dead("Man, learn to type a number.")


I just want to know because I've done some of the extra credit, but one of the questions is what are the bugs in those lines of code, having to do with using "0" and "1" to check for an integer and IMO I also want to know why please?

Thanks.

Best,

ghostheadx2

The other extra credit was easy though. lol.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by tgoe on Tue Mar 31, 2015 10:25 pm
([msg=87518]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

You're stuck on fundamentals.

Code: Select all
>>> "0" == 0
False


Why is this False? :)
User avatar
tgoe
Contributor
Contributor
 
Posts: 716
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by cyberdrain on Wed Apr 01, 2015 8:52 am
([msg=87523]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

Actually that is done correctly in this code, but still a valid question. Another one: what happens if you input 2?
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by ghostheadx2 on Wed Apr 01, 2015 3:33 pm
([msg=87528]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

tgoe wrote:You're stuck on fundamentals.

Code: Select all
>>> "0" == 0
False


Why is this False? :)


But when I try to use the actual integers, I get another error. That's weird. And also, I suspected that when looking at the code. I didn't think that it mattered bc of the other error I would get. Lol.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by cyberdrain on Wed Apr 01, 2015 4:32 pm
([msg=87529]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

ghostheadx2 wrote:But when I try to use the actual integers, I get another error. That's weird. And also, I suspected that when looking at the code. I didn't think that it mattered bc of the other error I would get. Lol.

Of course you do, if I recall this code correctly 'next' was a string here. Heh, I already gave a hint, I'll leave the rest to you :)
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by tgoe on Fri Apr 03, 2015 5:38 pm
([msg=87574]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

Nah, that's not really correct. I don't know the context of this code but I think the idea is to get something a bit more robust.

Something like this might work:
Code: Select all
def process_inputs(inputs):
    for n in inputs:
        try:
            i = int(n)
        except ValueError:
            print("Bad!: %s" % n)
        else:
            print("Good: %d" % i)


process_inputs(["123", "456k", 789, "1O1", "53", "h1 th3r3", 31])
print("")
process_inputs("9hf6sd7")
User avatar
tgoe
Contributor
Contributor
 
Posts: 716
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by cyberdrain on Fri Apr 03, 2015 8:38 pm
([msg=87576]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

Yup, you just gave the answer away... The idea of those extra credits is for people to search for information and learn things themselves. Instead you're just spoonfeeding ghostheadx2 :roll:

@ghostheadx2: the general way I debug programs is giving different types of input and see if it fails. Then you can start looking for the location the bug occurred and fix it. So in this case, you could input 2 and found the program returns it's not a number. Obviously that's not correct. While you could check if any of the 0-9 numbers are in the number, I think the try-block is also given as a hint for doing this on the LPTHW site.

The code tgoe gave is similar to what you should have ended up with. The try-block tries to evaluate everything that comes after it. If it can't evaluate something and e.g. in this case finds a ValueError (the input cannot be converted to a number), it will execute the 'except ValueError' clause. If there are no errors the 'else' clause is executed instead. Finally the 'finally' clause is always executed (both after except and after else), but that one isn't included here. You could use it to finish up some code that should always run, like clean-up code, but in practice it's rarely used.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by tgoe on Mon Apr 06, 2015 10:06 am
([msg=87611]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

Hey man, I only posted code cuz I felt you were leading him in the wrong direction. Checking for 0-9 or "0-9" doesn't seem very good. Having no context myself, I figured he'd have to understand and adapt the code anyway. Sorry for the spoilers :)
User avatar
tgoe
Contributor
Contributor
 
Posts: 716
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Are there any bugs in doing this? LPTHW Question

Post by cyberdrain on Mon Apr 06, 2015 11:14 am
([msg=87612]see Re: Are there any bugs in doing this? LPTHW Question[/msg])

tgoe wrote:Hey man, I only posted code cuz I felt you were leading him in the wrong direction. Checking for 0-9 or "0-9" doesn't seem very good. Having no context myself, I figured he'd have to understand and adapt the code anyway. Sorry for the spoilers :)

It's all good and you were right, I probably shouldn't expect people to go from checking 2 and "0-9" all the way to figuring out try blocks and int(num) by themselves.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron