Function returns old value

Bischoop Bischoop at vimart.net
Fri Dec 11 21:09:15 EST 2020


On 2020-12-12, dn <PythonList at DancesWithMice.info> 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
													0



More information about the Python-list mailing list