# HELP WITH COUNT FUNCTION

Page 1 of 1

## 6 Replies - 279 Views - Last Post: 19 June 2013 - 11:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323169&amp;s=1087a23c9eeb5af59923d9097aba8d41&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Holden

Reputation: 0
• Posts: 6
• Joined: 18-January 13

# HELP WITH COUNT FUNCTION

Posted 17 June 2013 - 06:48 PM

```def donuts(count):
if count < '10'
return 'Number of donuts: count'
else:
if count > '10'
return 'Number of donuts: many'

```

SyntaxError: invalid syntax

Could someone please tell me why it wont let me set count < '10' ???

This post has been edited by macosxnerd101: 17 June 2013 - 06:51 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: HELP WITH COUNT FUNCTION

### #2 macosxnerd101

• Self-Trained Economist

Reputation: 10488
• Posts: 38,871
• Joined: 27-December 08

## Re: HELP WITH COUNT FUNCTION

Posted 17 June 2013 - 06:51 PM

I'm not a Python guy, but I'd wager a guess that you shouldn't have quotes around 10.

### #3 Holden

Reputation: 0
• Posts: 6
• Joined: 18-January 13

## Re: HELP WITH COUNT FUNCTION

Posted 17 June 2013 - 06:56 PM

macosxnerd101, on 17 June 2013 - 06:51 PM, said:

I'm not a Python guy, but I'd wager a guess that you shouldn't have quotes around 10.

I just tried that, and it didnt high light anything, but it still said "invalid syntax"

### #4 Mekire

Reputation: 116
• Posts: 212
• Joined: 11-January 13

## Re: HELP WITH COUNT FUNCTION

Posted 17 June 2013 - 08:38 PM

Holden said:

I just tried that, and it didnt high light anything, but it still said "invalid syntax"

You need ":"s after if statements and the following lines must be indented:
```def donuts(count):
if count < 10:
print("Number of donuts: {}".format(count))
else:
print('Number of donuts: Many')
```

-Mek

### #5 woooee

Reputation: 21
• Posts: 78
• Joined: 21-November 12

## Re: HELP WITH COUNT FUNCTION

Posted 19 June 2013 - 09:46 AM

And if there are 10 doughnuts, None is returned because you don't allow for that in your code. You might want to take a look at http://www.tutorials...n_functions.htm, which, among other things, says

Quote

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
which is the default "return" in Python when there is no other in the code.

This post has been edited by woooee: 19 June 2013 - 09:52 AM

### #6 Holden

Reputation: 0
• Posts: 6
• Joined: 18-January 13

## Re: HELP WITH COUNT FUNCTION

Posted 19 June 2013 - 07:36 PM

Mekire, on 17 June 2013 - 08:38 PM, said:

Holden said:

I just tried that, and it didnt high light anything, but it still said "invalid syntax"

You need ":"s after if statements and the following lines must be indented:
```def donuts(count):
if count < 10:
print("Number of donuts: {}".format(count))
else:
print('Number of donuts: Many')
```

-Mek

Thank you, that was really useful

### #7 chan 06

Reputation: 2
• Posts: 63
• Joined: 07-October 12

## Re: HELP WITH COUNT FUNCTION

Posted 19 June 2013 - 11:18 PM

That is one way to do string formatting that Mekire has shown.

Mekire, on 17 June 2013 - 08:38 PM, said:

Holden said:

I just tried that, and it didnt high light anything, but it still said "invalid syntax"

You need ":"s after if statements and the following lines must be indented:
```def donuts(count):
if count < 10:
print("Number of donuts: {}".format(count))
else:
print('Number of donuts: Many')
```

-Mek

Also your code is missing columns, tabs and a lot of things that makes up the program. Therefore you would get invalid syntax, ensure to add columns, brackets or tabs in your program to avoid syntax error.

```def donuts(count):
# It is important to add a column when you are making if-statemnts arguemnets
if count < 10:
# Add a tab since it is part of the arguemnt
# String format it %d equivelant to decimal numbers (...-3,-2,-1,0, 1, 2, 3...)
return "Number of donuts: %d" % (count)
if count > 10:
return "Number of donuts: many"

print(donuts(5))
```

Here is another version of string formatting, notice that I used %d that represents decimal numbers. Other useful ones are:

%.2f formats your float numbers in 2 decimal numbers.

Learn the basics of programming before you start learning methods and functions, if you lack the knowledge your going to have hard time programming.

This post has been edited by chan 06: 19 June 2013 - 11:21 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }