user1186154
user1186154

Reputation: 1

Odd Even String- Python

Write a function named evenOddString() that takes a single string parameter.

If the length of the string parameter:

This is my code so far:

def evenOddString():
    len(var) % 2 != 0

Upvotes: 0

Views: 6703

Answers (3)

DonCallisto
DonCallisto

Reputation: 29942

def control4OddString(myString):
    if len(myString) % 2: return myString
    else: return myString+myString

Upvotes: 0

gfortune
gfortune

Reputation: 2629

First, your evenOddString() function needs to take a parameter. For example:

def evenOddString(the_string_parameter):
  print(the_string_parameter)

To call that function, you would have something like:

evenOddString('abc123')

which would print out abc123 on the console. In your case, you will want check the length of the_string_parameter and do some stuff with it "if" the length is even or odd. The if and elif statements will be helpful. See http://docs.python.org/tutorial/controlflow.html for docs on flow control.

You also want to return a value back out of your function. If I wanted to return the string unchanged, I would do something like:

def evenOddString(the_string_parameter):
  return the_string_parameter

Note that I'm being a little vague here as this sounds like a homework assignment and I don't want to simply write all the code for you. Since you sound like a new programmer just starting out and in need of a good tutorial, I highly recommend you work through http://learnpythonthehardway.org/

Upvotes: 3

inspectorG4dget
inspectorG4dget

Reputation: 114035

  1. define a function that take a parameter - Your definition does not take a parameter
  2. get the length of that parameter - No problems here
  3. if the length is odd, return the return the parameter - Your condition is correct, but you're not doing anything with the condition
  4. else: return the parameter concatenated with itself - not implemented

Upvotes: 2

Related Questions