manuleka

The Return Statement

Recommended Posts

def test1():x = input('1 or 2').strip()if x == '1':x = 'left'return xelif x == '2':x = 'right'return xelse:print('Error!')test1()def main():x = test1()print(x)if __name__ == '__main__':main()

now just needing some enlightening - i still don't get why i get a return 'None' after running the else portion of the code
print('Error!')print(x)

if i enter 1 or 2 on first execution left or right is printed, but if input anything other then 1 or 2 and then enter 1 or 2 the latest selection isn't assigned to x...

can someone explain why please and how to ensure the returned value is the latest input/assignment?
Edited by manuleka

Share this post


Link to post
Share on other sites

fixed it... the above code didn't apply anything to x when the else statement is executed

so just added x = test1()

def test1():   x = input('1 or 2').strip()   if x == '1':      x = 'left'      return x   elif x == '2':      x = 'right'      return x   else:      print('Error!')      x = test1()def main():x = test1()print(x)if __name__ == '__main__':main()

Share this post


Link to post
Share on other sites

I know this is old, but I haven't been on much lately so I'm trying to change that.

 

So what were you trying to achieve?

 

From your first post:

 

You have a function called test1 which takes user input (you should not use input but raw_input, input is like calling eval(raw_input()) and eval is evil in most circumstances) in which you're asking them to enter '1' or '2'.

 

You then have an if statement that checks if '1' was entered and if so, x will be reassigned to 'left' or if x is '2', reassign it to 'right'.

 

If none of those conditions are met, you'll print 'Error!' and then you will recursively execute test1() again but you miss returning it.

 

I don't see how assigning test1() to x in that else statement returns the correct result as you don't return x. return test1() should have been the answer to fix that problem as you have no return statement in your else section, which should implicitly return None. If not, then this would not be normal.

 

An alternative way to write this could be:

 

#!/usr/bin/env python# -*- coding: utf-8 -*-def test1():x = raw_input('Enter 1 or 2: ').strip()while x not in ('1', '2'):	 x = raw_input('Warning, You must enter 1 or 2: ').strip()return ('left', 'right')[int(x) - 1]def main():x = test1()print xif __name__ == '__main__':main()

This is written for Python 2.7

 

What we have here is similar, however I used a while loop to ensure we only exit the loop if x is either '1' or '2', if not, a warning is printed and it'll ask again. The reason I'm doing this is that I like functions to try and only have one point of return/exit if necessary, but this is really your choice and a habit I tried keeping to with C programming.

 

When we exit the loop, I then just return either 'left' or 'right' from the tuple by calculating the index by the answer that was supplied to x.

 

Cheers,

 

 

MC

Share this post


Link to post
Share on other sites

Kiwi indeed.And the oldest semi-active member here apart from OpaQue.I just wish I could share more knowledge here, so I'm hoping I'll be here more often.Cheers,MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now