6 Replies - 319 Views - Last Post: 19 June 2013 - 11:18 PM Rate Topic: -----

#1 Holden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,280
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Holden  Icon User is offline

  • New D.I.C Head

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

Re: HELP WITH COUNT FUNCTION

Posted 17 June 2013 - 06:56 PM

View Postmacosxnerd101, 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"
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • 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
Was This Post Helpful? 1
  • +
  • -

#5 woooee  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 109
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 Holden  Icon User is offline

  • New D.I.C Head

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

Re: HELP WITH COUNT FUNCTION

Posted 19 June 2013 - 07:36 PM

View PostMekire, 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
Was This Post Helpful? 0
  • +
  • -

#7 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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.

View PostMekire, 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"

# Test your program.
print(donuts(5))


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

%s formats your string
%.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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1