1

I'm trying to write a program that asks the user for a string input without using global variables. If the string has parentheses only side by side, then it's even. if it has letters, numbers, or the parentheses are spaced out, then it's uneven. For example, () and ()() is even, whereas (() and (pie) is not. Below is what I've written so far. Do I have to create more than one function for this problem?

def recursion():
    string = str(input("Enter your string: "))
    if string == "(" or ")":
        print("The string is even.")
    else:
        print("The string is not even.")
Antti Haapala
  • 117,318
  • 21
  • 243
  • 279
user2581724
  • 221
  • 3
  • 8
  • 18
  • I think you need to read book on introductory Python. This problem can be solved in a single function, but it is going to use recursive. – Hunter McMillen Aug 02 '13 at 17:27
  • 2
    Your `if` condition would always evaluate to `True` since you have a non empty string `")"` in the `or` condition. – Sukrit Kalra Aug 02 '13 at 17:29
  • you should think of your algorithm first a bit further. As it is now the program hardly works at all. – Antti Haapala Aug 02 '13 at 17:29
  • what are you trying to do? I am confused as to the point is. What you are trying to do. Is this an assignment? if so why don't you link it so we have an idea of what you are trying to do. – Tall Paul Aug 02 '13 at 17:35
  • 1
    Ummm.... `even = not string.replace('()', '')` meets the spec. as I understand it... – Jon Clements Aug 02 '13 at 17:35
  • Also, to expand on what Sukrit is saying, your if statement is saying "If string == "(" or ")" " which is read as "If string == "(" " or " ")" " and since ")" is a non-empty string, it will register as True. Meaning: "if string == "(" or True)" Just adding clarity, because you seem to be even newer to this business than I am. – CamelopardalisRex Aug 02 '13 at 17:54
  • @AlexBaldwin actually the code I posted would give odd for `(()`... – Jon Clements Aug 02 '13 at 18:11
  • 1
    Guys, why downvote this question? The OP has clearly asked what they want and shown some work, they just don't have a depth of knowledge of programming. Let's not make SO so unfriendly to new posters that make an effort of asking a good question. – SethMMorton Aug 02 '13 at 18:11
  • @Jon, You're right, I totally misunderstood what was going you. You are quite correct and I am a humble novice, making errors everywhere I go! I deleted my comment. It was utterly useless. – CamelopardalisRex Aug 02 '13 at 18:14
  • possible duplicate of [Turning iteration into recursion](http://stackoverflow.com/questions/18006806/turning-iteration-into-recursion) – Sylwester Aug 02 '13 at 20:31

3 Answers3

1

A really useful stdlib script shlex provides this type of parsing automatically and allows you to customize the behavior.

synthesizerpatel
  • 24,615
  • 4
  • 70
  • 85
  • Wow, I had no idea about this. Thank you. Amazing how much of the language I use every day I really don't know that is hidden under the covers... – jdotjdot Aug 05 '13 at 00:14
0

I will collect much of the information in the comments into an answer.

First, in your posted code the line

if string == "(" or ")":

will always evaluate to True because a non-empty string is always True. What you have written is equivalent to:

if ( string == "(" ) or ")":

which is therefore equivalent to

if ( string == "(" ) or True:

which is always True.

Next, since it seems that you simply want to check if your string consists only of sets of '()', you can use Jon Clements' suggestion of not string.replace('()',''):

if not string.replace('()', ''):

Let's take a look at what this does:

>>> not '()'.replace('()', '')
True
>>> not '()()'.replace('()', '')
True
>>> not '(()'.replace('()', '')
False
>>> not '(pie)'.replace('()', '')
False

Last, you shouldn't call a variable string because it shadows a module in the standard library. Something like user_given_string might work.

Summing it all up:

def recursion():
    user_given_string = input("Enter your string: ")
    if not user_given_string.replace('()', ''):
        print("The string is even.")
    else:
        print("The string is not even.")
SethMMorton
  • 36,611
  • 12
  • 59
  • 76
0

No, you do not need to make multiple functions for this. In fact, I would personally just do this:

def recursion():
    print("The string is not even." if input("Enter your string: ").replace('()','') else "The string is even.")

There really isn't a need to have a 6 line function for this job. Instead, use a ternary statement like I did to keep it concise.

Also, just wanted to mention this, there is no need to do:

str(input())

because input always returns a string in Python 3.x.

Community
  • 1
  • 1