is_friend (Unit 2, 18)

Hello:

I responded to the subject quiz using the following:
def is_friend(name):
initial=name[0]
return initial=='d'

However, when I submit, it keeps giving me an error message stating that it didn't work on the string "Dave". However, it does work on "Dave" and numerous other strings (names) that I tested.

I would appreciate if someone could tell me what is wrong with the above. I saw the answer in the video that follows the quiz, and I understand that there are other (better) ways to do it, but what is wrong with this way?

Thanks

Hassan

asked 29 Feb '12, 03:36

hassan210977's gravatar image

hassan210977
1613
accept rate: 0%

retagged 29 Feb '12, 03:41

dreyescat's gravatar image

dreyescat
6.9k163482


6 Answers:

I think you are comparing 'd' against 'D'. They are not the same.

link

answered 29 Feb '12, 03:40

dreyescat's gravatar image

dreyescat
6.9k163482

You are checking if the first letter is a 'd' instead of a Capital 'D'... I guess that is your problem...
you see the programm is case sensitive so right now it is checking if 'D' is the same as 'd'

converting to values:
'D' == 'd'

and they have different values, try to look for a ASCII Table which has the values of the characters for a computer and you'll see that they are in fact different.

What you could do as alternative is something like:

if ( initial=='d' or initial == 'D'):
    return True
else:
    return False
link

answered 29 Feb '12, 03:42

Flavio%20Diez's gravatar image

Flavio Diez
23210

Python is case sensative. If you wanted to (as I did) you could take both upper and lower case into account in your solution

link

answered 29 Feb '12, 03:44

Rob%20Barnes-3's gravatar image

Rob Barnes-3 ♦
19.1k1068205

Thanks everyone. It just hit me, and that was it. Thanks again.

link

answered 29 Feb '12, 03:53

hassan210977's gravatar image

hassan210977
1613

I got the same problem, but for me it was because I gave True and False as a string, not as a boolean.

link

answered 29 Feb '12, 04:08

Justin%20Nichol's gravatar image

Justin Nichol
52

yup..same here

(01 Mar '12, 12:16) Arushi Pant-1 Arushi%20Pant-1's gravatar image

There is another problem with using name[0]. If then name provided is am empty string, the program will get an error. It would be much better to use name[:1] instead, which should successfully work even for empty strings.

link

answered 01 Mar '12, 11:43

Vadim%20Dribinsky's gravatar image

Vadim Dribinsky
1787

Thanks for the tip :-)

(01 Mar '12, 19:10) hassan210977 hassan210977's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,321
×20
×8

Asked: 29 Feb '12, 03:36

Seen: 280 times

Last updated: 01 Mar '12, 19:10