Planet Python
Has something happened to the Planet Python feed? - Last update: December 07, 2020 04:48 PM UTC -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Function returns old value
I've function asking question and comparing it, if is not matching 'yes' it does call itself to ask question again. The problem is that when function is called second time it returns old value or with additional else statement it returns none. Code: https://bpa.st/KVGA How this functions should look properly? -- Thanks in advance -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On 12/12/2020 14:25, Bischoop wrote: I've function asking question and comparing it, if is not matching 'yes' it does call itself to ask question again. The problem is that when function is called second time it returns old value or with additional else statement it returns none. Code: https://bpa.st/KVGA How this functions should look properly? In the event of "yes" the function returns a value (return ask). When the function calls itself, what happens to the return-ed value? -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Letter replacer - suggestions?
On 2020-12-07, MRAB wrote: > > word = input( f'input word you want to change letters in: ') Yes, I've learn already that should use only when want to use variables I just started using this new f' string method. > > There's no need for the f prefix. > > > print(f' Your word to change: ,{word}') > > Is the comma a typo? > lol mixing old and new methods, just old habit. > > > change_this_list = list(change_this) > > replace_with_list = list(replace_with) > > > > while True: > > try: > > for element in word_list: > > for x in change_this_list: > > if element == x: > > to = word_list.index(element) > > replace = change_this_list.index(x) > > word_list[to] = replace_with_list[replace] > > new_word = ''.join(word_list) > > print(f'{new_word}') > > > You can make it a lot shorter and faster by using a dict. The entire > while loop section can be replaced with: > > replacements = dict(zip(change_this, replace_with)) > new_word = ''.join(replacements.get(letter, letter) for letter in word) > print(new_word Simply and nice, I wouldn't come with that yet at all, beyond my knowledge. Thanks a lot for feedback and suggestions. -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
Bischoop writes: > I've function asking question and comparing it, if is not matching 'yes' > it does call itself to ask question again. The problem is that when > function is called second time it returns old value or with additional > else statement it returns none. > > Code: https://bpa.st/KVGA It calls itself again, but what does it return in that case? -- https://mail.python.org/mailman/listinfo/python-list
Re: Letter replacer - suggestions?
On 2020-12-07, Marco Sulla wrote: > Not sure why you want to do this (it's schoolwork)? >Anyway, this is my version: > Thanks, seems nicer I think not to mention those exceptions. So far I'm just learning and doing everythin only for myself so do not think on others users but I know it's good to make a habit. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
It won't return until the inner call to question (and it's not using the return value on inner call). Eventually, (and not until you answer yes) it will return the first answer. On Fri, 2020-12-11 at 18:55 -0700, Joe Pfeiffer wrote: > Bischoop writes: > > > I've function asking question and comparing it, if is not matching > > 'yes' > > it does call itself to ask question again. The problem is that when > > function is called second time it returns old value or with > > additional > > else statement it returns none. > > > > Code: https://bpa.st/KVGA > > It calls itself again, but what does it return in that case? -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On 2020-12-12, dn wrote: > On 12/12/2020 14:25, Bischoop wrote: >> >> >> >> I've function asking question and comparing it, if is not matching 'yes' >> it does call itself to ask question again. The problem is that when >> function is called second time it returns old value or with additional >> else statement it returns none. >> >> Code: https://bpa.st/KVGA >> >> How this functions should look properly? > > > In the event of "yes" the function returns a value (return ask). > When the function calls itself, what happens to the return-ed value? Well I've put the output in a paste as well, anyway here is what is in a paste: def question(): ask = input("Are you OK?:").lower() if ask != 'yes': question() return ask print (question()) #output: Are you OK?:no Are you OK?:no Are you OK?:yes no --- #Another way with 'elif' statment returns none def question(): ask = input("Are you OK?:").lower() if ask != 'yes': question() elif ask == 'yes': return ask print (question()) #output: Are you OK?:no Are you OK?:yes None #Tried also nested functions, same results: def question(): ask = input("Are you OK?:").lower() def check_question(n): if ask != 'yes': question() else: return ask m = check_question(ask) print (m) question() #output: Are you OK?:no Are you OK?:yes None Process finished with exit code
Re: Function returns old value
Sorry, actually, if you do not answer yes, will always return None, not the first answer as I suggested. On Fri, 2020-12-11 at 18:55 -0700, Joe Pfeiffer wrote: > Bischoop writes: > > > I've function asking question and comparing it, if is not matching > > 'yes' > > it does call itself to ask question again. The problem is that when > > function is called second time it returns old value or with > > additional > > else statement it returns none. > > > > Code: https://bpa.st/KVGA > > It calls itself again, but what does it return in that case? -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On 2020-12-12, Joe Pfeiffer wrote: > Bischoop writes: > >> I've function asking question and comparing it, if is not matching 'yes' >> it does call itself to ask question again. The problem is that when >> function is called second time it returns old value or with additional >> else statement it returns none. >> >> Code: https://bpa.st/KVGA > > It calls itself again, but what does it return in that case? I've stated it returns old value that I've input first time, anyway output is also inluded in a paste but since you've asked: def question(): ask = input("Are you OK?:").lower() if ask != 'yes': question() return ask print (question()) #output: Are you OK?:no Are you OK?:no Are you OK?:yes no --- #Another way with 'elif' statment returns none def question(): ask = input("Are you OK?:").lower() if ask != 'yes': question() elif ask == 'yes': return ask print (question()) #output: Are you OK?:no Are you OK?:yes None #Tried also nested functions, same results: def question(): ask = input("Are you OK?:").lower() def check_question(n): if ask != 'yes': question() else: return ask m = check_question(ask) print (m) question() #output: Are you OK?:no Are you OK?:yes None Process finished with exit code 0 -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On 12/12/2020 15:09, Bischoop wrote: On 2020-12-12, dn wrote: On 12/12/2020 14:25, Bischoop wrote: I've function asking question and comparing it, if is not matching 'yes' it does call itself to ask question again. The problem is that when function is called second time it returns old value or with additional else statement it returns none. Code: https://bpa.st/KVGA How this functions should look properly? In the event of "yes" the function returns a value (return ask). When the function calls itself, what happens to the return-ed value? Apologies, the question was for you to ask yourself; not me asking for me! When you read the code, to where are the results of the 'inner call' assigned? If you can answer this question, you will solve 'the problem'! (and have learned an approach to solving problems in-future) Well I've put the output in a paste as well, anyway here is what is in a paste: Thank you. Speaking personally, I prefer the code to be included in the email. However, it would be better to use spaces rather than tabs (Python's preferred style, per PEP-8) because many email packages implement tab-stops as eight-spaces apart. -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On Sat, Dec 12, 2020 at 1:23 PM dn via Python-list wrote: > > Speaking personally, I prefer the code to be included in the email. > However, it would be better to use spaces rather than tabs (Python's > preferred style, per PEP-8) because many email packages implement > tab-stops as eight-spaces apart. > Be careful of wording here. PEP 8 does not specify that all Python code should use spaces rather than tabs. Tabs are absolutely fine for your code. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On 12/12/2020 15:25, Chris Angelico wrote: On Sat, Dec 12, 2020 at 1:23 PM dn via Python-list wrote: Speaking personally, I prefer the code to be included in the email. However, it would be better to use spaces rather than tabs (Python's preferred style, per PEP-8) because many email packages implement tab-stops as eight-spaces apart. Be careful of wording here. PEP 8 does not specify that all Python code should use spaces rather than tabs. Tabs are absolutely fine for your code. Hence "preferred", cf "prescribed" or "required". My personal reaction when ppl do make such a demand, is to use the facility built-in to decent editors/IDEs to change from one to the other - no fuss, no muss; peace reigns amongst the team once again...! Wish could do that with such ease in other applications! Nevertheless, email-client observation applies... -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On Sat, Dec 12, 2020 at 1:39 PM dn via Python-list wrote: > > On 12/12/2020 15:25, Chris Angelico wrote: > > On Sat, Dec 12, 2020 at 1:23 PM dn via Python-list > > wrote: > >> > >> Speaking personally, I prefer the code to be included in the email. > >> However, it would be better to use spaces rather than tabs (Python's > >> preferred style, per PEP-8) because many email packages implement > >> tab-stops as eight-spaces apart. > >> > > > > Be careful of wording here. PEP 8 does not specify that all Python > > code should use spaces rather than tabs. Tabs are absolutely fine for > > your code. > > Hence "preferred", cf "prescribed" or "required". It's not even that. It's required for the standard library, and not at all a language recommendation elsewhere. > My personal reaction when ppl do make such a demand, is to use the > facility built-in to decent editors/IDEs to change from one to the other > - no fuss, no muss; peace reigns amongst the team once again...! > > Wish could do that with such ease in other applications! Ah yes, and then you have these massive source control diffs when you do a meaningless conversion over large slabs of the code! Perfect, just what we wanted. For teams that disagree on the correct width of indentation levels, why not standardize on a single token meaning "indentation level", and then allow everyone to configure their editors to show it at whatever width they prefer? > Nevertheless, email-client observation applies... Yes. Make sure you use an email client that supports leading whitespace of all forms. Otherwise you're not going to have a good time of it. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Function returns old value
On 12/11/2020 8:25 PM, Bischoop wrote: I've function asking question and comparing it, if is not matching 'yes' it does call itself to ask question again. The problem is that when function is called second time it returns old value or with additional else statement it returns none. Code: https://bpa.st/?? Don't post links to unknown sites. Reduce it to the minimum needed to exhibit the questionable behavior and include inline with the question. How this functions should look properly? -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Letter replacer - suggestions?
On 2020-12-07, Grant Edwards wrote: > On 2020-12-07, MRAB wrote: > >> Avoid a 'bare' except unless you _really_ mean it, which is >> virtually never. Catch only those exceptions that you're going to >> handle. > > And sometimes "handling" is just printing some extra stuff and then > re-raising the original exception: > > try: > something(): > except: > print() > raise > > Noted, thanks. -- https://mail.python.org/mailman/listinfo/python-list
Returning from a multiple stacked call at once
Hello In case a function recursively calls itself many times, is there a way to return a data immediately without unstacking all functions ? -- https://mail.python.org/mailman/listinfo/python-list