Very beginner help with defining a function

This is the place for queries that don't fit in any of the other categories.

Very beginner help with defining a function

Postby g2knee » Tue Aug 20, 2013 7:56 pm

Hello,

(Screenshot below)

I'm just learning python from literally the ground up, and I'm trying to complete an "exercise" and I seem to be getting a weird bug. I know that when you're defining a function, you just leave a blank line to stop the definition. However, when I try to do this for a particular definition, it just keeps going and going and won't let me stop no matter how many blank lines I enter. Other definitions work just fine. The screenshot will show what I mean. The top one is the function I'm trying to write. The bottom screenie is an alternate function that works properly but is not as accurate for what I need.

http://i.imgur.com/2S7mXTY.jpg

In case you're wondering, the exercise is thus:
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.

If someone can please help me know what is going on here, and why I can't STOP writing this particular function, I would be grateful! It's probably something obvious I'm missing, so maybe I can learn something.

Thanks!
Last edited by g2knee on Tue Aug 20, 2013 8:09 pm, edited 1 time in total.
g2knee
 
Posts: 2
Joined: Tue Aug 20, 2013 7:49 pm

Re: Very beginner help with defining a function

Postby Yoriz » Tue Aug 20, 2013 8:09 pm

Hi,
Welcome to the forum, please read the new user read this link in my signature

g2knee wrote:I know that when you're defining a function, you just leave a blank line to stop the definition.

It is not a blank space that ends a function, it is the indentation(or the dedentation i guess you would call it).

Edit: Note i just noticed in the first code you have uneven brackets, there are 3 open brackets and only 2 closing.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 726
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Very beginner help with defining a function

Postby g2knee » Tue Aug 20, 2013 8:20 pm

So now I've learned uneven brackets will not let you stop a definition until they are closed. I figured it just would have given me an error message so I didn't think to look closely at that!

This is going to be a long process of learning. Thanks for your help. :)
g2knee
 
Posts: 2
Joined: Tue Aug 20, 2013 7:49 pm

Re: Very beginner help with defining a function

Postby Yoriz » Tue Aug 20, 2013 8:24 pm

g2knee wrote:I figured it just would have given me an error message

In a ide it would do, but the command line is expecting a close bracket.

Consider this bit of code
Code: Select all
print 'bug'.rjust(70)
Outputs
Code: Select all
                                                                   bug
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 726
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests