Reputation: 73
I have a string s="expences > drinks"
that is sent to a function. Inside that function I am supposed to add new values using raw_input()
till I input an empty string.
Each one of these inputs should be added to the initial string. In the case of an empty string, the function must return the string with all of the previously added words.
I tried to realize it by calling the function from itself and pass it the changed string but it just returns with only the first added word. I also tried avoid calling the function from itself using while True:
and if empty string then break the loop and return the value. But none of these variants works properly.
Please explain what I do wrong and what is the most pythonic way of doing this.
Here's the code:
#!/usr/bin/env python -tt
# -*- coding: utf-8 -*-
def create_sub_cat(parent):
print "creating subcat\n %s > " %(parent)
conf=raw_input("type new subcat or press \"ENTER\" to save changes\n")
if conf!="":
parent+=" > "+conf
create_sub_cat(parent)
return parent
s="expences > drinks"
print create_sub_cat(s)
Upvotes: 1
Views: 91
Reputation: 180441
You can use iter
with a for loop, you don't need to keep calling the function, iter takes a sentinel value as it's last arg which it will keep looping until the sentinel value is entered:
def create_sub_cat(parent):
print "creating subcat\n {} > ".format(parent)
for conf in iter(lambda: raw_input("type new subcat or"
" press \"ENTER\" to save changes\n"),""): # empty string for sentinel value
parent += " > " + conf
return parent
Or using a while True:
def create_sub_cat(parent):
print "creating subcat\n {} > ".format(parent)
while True:
conf = raw_input("type new subcat or"
" press \"ENTER\" to save changes\n")
if not conf: # if user enters empty string break
break
parent +=" > " + conf # else keep concatenating
return parent
Why your code does not work is because you don't return the recursive calls so you only get the first value:
def create_sub_cat(parent):
print "creating subcat\n %s > " %(parent)
conf=raw_input("type new subcat or press \"ENTER\" to save changes\n")
if conf!="":
parent+=" > "+conf
return create_sub_cat(parent) # return
return parent
Upvotes: 1