10 Replies - 653 Views - Last Post: 24 August 2016 - 03:55 AM Rate Topic: -----

#1 unfixtoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 16

len statement doesn't work on function

Posted 24 August 2016 - 01:33 AM

Hey guys! This is my first post here....

well the question is Write a function named right_justify that takes a string named s as a parameter and prints the
string with enough leading spaces so that the last letter of the string is in column 70 of the display.

So i understand the question but i don't know how to make the string comes after 70 spaces.

What i did:

>>>def var(mod):
len(mod)

So after defining the function i called the function but it didn't work, surprisingly no error were shown either.
I tried doing len statement outside the function and i worked.

>>>len('abcdefg')
>>>7

i tried in loops and it worked:

>>> mod='monkey'
>>> for i in mod:
len(mod)


6
6
6
6
6
6

then i tried the same loop in a function:
>>> def var():
mod='monkey'
for i in mod:
len(mod)

it didn't worked ....no error were shown either.


I just don't understand why len statement doesn't work on function.... and please teach me how to solve the question.thank you

Is This A Good Question/Topic? 0
  • +

Replies To: len statement doesn't work on function

#2 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,722
  • Joined: 12-December 12

Re: len statement doesn't work on function

Posted 24 August 2016 - 01:46 AM

len() is working, but len(mod) on its own is an empty statement; it determines the length but does nothing with the result. You either want to print the result or store it somewhere - in a variable - for later use.

Quote

and please teach me how to solve the question.

How would you solve your task? What ideas do you have?

Note that the entered string needs to fit within the 70 characters. I.e. if the string has a length of 10 then the other 60 characters will be spaces.
Was This Post Helpful? 1
  • +
  • -

#3 unfixtoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 16

Re: len statement doesn't work on function

Posted 24 August 2016 - 01:58 AM

Thank you sir i will keep trying what you say.

This post has been edited by andrewsw: 24 August 2016 - 02:12 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 unfixtoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 16

Re: len statement doesn't work on function

Posted 24 August 2016 - 02:04 AM

You said i can print the results .....but how ?

I tired:
>>> def var(mod):
len(mod)
print(mod)

>>> var('Hello')
Hello

This is not what i want to get.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,722
  • Joined: 12-December 12

Re: len statement doesn't work on function

Posted 24 August 2016 - 02:14 AM

That is behaving as expected, it prints the value of mod.

If you want to print the length then you could do print(len(mod)) but presumably you want to do something with the length, so (after having printed its value to confirm it's working) store its value in a variable.
Was This Post Helpful? 1
  • +
  • -

#6 unfixtoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 16

Re: len statement doesn't work on function

Posted 24 August 2016 - 02:25 AM

Thank you sir ! i didn't knew i have to print like that. Sir why would i have to store the value?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,722
  • Joined: 12-December 12

Re: len statement doesn't work on function

Posted 24 August 2016 - 02:27 AM

What do you intend to do with the length, other than print it?
Was This Post Helpful? 1
  • +
  • -

#8 unfixtoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 16

Re: len statement doesn't work on function

Posted 24 August 2016 - 02:44 AM

Sir! I now understand. as you said i can either print or store the length.

at first i tired:
>>> def book(paper):
pen=len(paper)
print(len(paper))
print (str(pen).rjust(70,'.'))

>>> book('sidney')
6
.....................................................................6 ( i was like why do i get 2 '6') then i understood wht you said. i can either print or store the length.

>>> def book(paper):
pen=len(paper)
print (str(pen).rjust(70,'.'))

>>> book('sidney')
.....................................................................6

i found the rjust function on google ....and it worked but can i print the '6' without 70 '.' ? i meant i don't want the full-stop i just want it blank .
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,722
  • Joined: 12-December 12

Re: len statement doesn't work on function

Posted 24 August 2016 - 02:58 AM

You mean like a space " "?
Was This Post Helpful? 1
  • +
  • -

#10 unfixtoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 16

Re: len statement doesn't work on function

Posted 24 August 2016 - 03:02 AM

View Postandrewsw, on 24 August 2016 - 02:58 AM, said:

You mean like a space " "?


I got it sir.
>>> var1='no space'
>>> print(var1.rjust(10,' '))
no space


Thank you very much for helping me sir!
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,722
  • Joined: 12-December 12

Re: len statement doesn't work on function

Posted 24 August 2016 - 03:55 AM

Glad to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1