python help

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

python help

Post by War_Archer on Thu Jan 14, 2010 10:44 pm
([msg=33362]see python help[/msg])

say i have this code

Code: Select all
class noname(object):
    def something(self):
        print 'Whatever'
       
class noname1(object):
    def watever(self):
        m = noname()
        print 'abc ',m.something()
       
if __name__ == "__main__":
    s = noname1()
    s.watever()


it prints
abc Whatever
None

how do I get rid of the None that it prints? I am not suppose to have a return statement in the method something()
thanks
War_Archer
New User
New User
 
Posts: 4
Joined: Thu Jun 26, 2008 10:09 pm
Blog: View Blog (0)


Re: python help

Post by faazshift on Thu Jan 14, 2010 11:15 pm
([msg=33363]see Re: python help[/msg])

In the function whatever() you are trying to print 'abc ' and the return of m.something(), but in m.something() you are already printing 'Whatever'. To fix this, you could either put m.something() on the next line, or you could just return 'Whatever', instead of printing it.

Modified function 'something()' to give the desired result:
Code: Select all
    def something(self):
        return 'Whatever'
faazshift
Contributor
Contributor
 
Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Re: python help

Post by War_Archer on Thu Jan 14, 2010 11:22 pm
([msg=33364]see Re: python help[/msg])

faazshift wrote:In the function whatever() you are trying to print 'abc ' and the return of m.something(), but in m.something() you are already printing 'Whatever'. To fix this, you could either put m.something() on the next line, or you could just return 'Whatever', instead of printing it.

Modified function 'something()' to give the desired result:
Code: Select all
    def something(self):
        return 'Whatever'

hey thanks for the reply. it works when you put m.something() on the next line. but part of the requirement for this is from our teacher is that we are to print the the 'Whatever' in the method not return it.
War_Archer
New User
New User
 
Posts: 4
Joined: Thu Jun 26, 2008 10:09 pm
Blog: View Blog (0)


Re: python help

Post by sanddbox on Thu Jan 14, 2010 11:32 pm
([msg=33366]see Re: python help[/msg])

War_Archer wrote:
faazshift wrote:In the function whatever() you are trying to print 'abc ' and the return of m.something(), but in m.something() you are already printing 'Whatever'. To fix this, you could either put m.something() on the next line, or you could just return 'Whatever', instead of printing it.

Modified function 'something()' to give the desired result:
Code: Select all
    def something(self):
        return 'Whatever'

hey thanks for the reply. it works when you put m.something() on the next line. but part of the requirement for this is from our teacher is that we are to print the the 'Whatever' in the method not return it.

Returning it WILL print it since you are printing out what it returns.

If you're unable to do that, than just call m.something()
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests