[Tutor] learning to programming questions part 2

Alan Gauld alan.gauld at btinternet.com
Wed Jul 2 00:05:50 CEST 2014


On 01/07/14 20:39, keith papa wrote:

> read both chapters 1 and 2 about string and variables and keywords., so
> I decide to look online for some practice to help me and I found
> something on http://www.pyschools.com/
>
>
> 1.Write the function countA(word) that takes in a word as argument and
> returns the number of 'a' in that word.

The problem is that you are still quite a long way from
being able to define your own functions. You need to find
some simpler examples

> The code starts out with
>
>>>>def countA(word):

This is how you define your own fuinctions in Python.
You need to write the code that does the counting of
the 'a's in the word and return the result.

Once you've done that you an call it as in the examples
provided.

Here is a simpler example:
Write a function, add2 that takes a number as input and returns the 
number plus 2.
Examples:

 >>> add2(3)
5
 >>> add2(8)
10

The code:

 >>> def add2(n):
...     return n+2

 >>> add2(4)
6

> 1. >>>def countA(apple):("a")
>
> 2. >>>print ("apple")
> def count('a')
>
> 3. >>>("apple").count("a")
>
> 4. >>>countA= apple.count ("a")
>
> #help me solve this

None of these are close because you haven't covered how to write 
functions yet.

That will appear (quite a bit) later in your tutorial.
To use a musical analogy, you have just learned the notes of
the scale and tried to do a homework that asks you to write
a song in waltz time.
It's too advanced for your current level of skill.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list