PDA

View Full Version : Python self learning



hdanap
March 20th, 2011, 02:59 AM
Hi guys,

1st thing i want to say, this is not an assignment im readn/learning python on my own.

i came across this problem, i really dont understand it, i know how to write doctest when it come to numbers but i really don't know what the book wants me to do, if i could be channeled in the right direction.

Question:

Write Python code to make each of the following doctests pass:
"""
>>> type(fruit)
<type 'str'>
>>> len(fruit)
8
>>> fruit[:3]
'ram'
"""

am i supposed to write a function or just write code in a script?

pls help

my attempt


def word(s):
"""
>>> type(fruit)
<type 'str'>
>>> len(fruit)
8
>>> fruit[:3]
'ram'
"""
fruit='ramadana'
if s == fruit:
x= type(fruit)
y= len(fruit)
z= fruit[:3]

cjhabs
March 20th, 2011, 03:06 AM
fruit = "xxxxxram"

Then run each of the commands and you'll get the response shown - i.e. it is an 8 character string ending in "ram"

hdanap
March 20th, 2011, 03:20 AM
when i do :

"""
>>> type(fruit)
<type 'str'>
>>> len(fruit)
8
>>> fruit[:3]
'ram'
"""
fruit= 'adanaram'
print type(fruit)
print len(fruit)
print fruit[:3]

i get from the interpreter:

<type 'str'>
8
ada
3 items had no tests:
__main__
__main__.find
__main__.without_vowels
0 tests in 3 items.
0 passed and 0 failed.
Test passed.

from this i understand that no test was executed, how do i make the test run???

hdanap
March 20th, 2011, 08:36 AM
i figured it out, this is the code that did the trick


fruit= 'ramadana'

def word(s):
"""
>>> type(fruit)
<type 'str'>
>>> len(fruit)
8
>>> fruit[:3]
'ram'
"""
#s=str(s)
#x=type(s)
#y=len(s)
#z=s[:3]
#print type(s)
#print len(s)
#print s[:3]


the commented code could be uncommented and the test still works

cjhabs
March 21st, 2011, 02:26 AM
You don't need print:
fruit= 'adanaram'
type(fruit)
len(fruit)
fruit[:3]