|
|
The Return Statement | ||
Discussion by manuleka with 5 Replies.
Last Update: January 29, 2013, 9:17 pm | |||
CODE
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!')
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
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?
Mon Aug 20, 2012 Reply New Discussion
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()
so just added x = test1()
CODE
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()
Sat Sep 22, 2012 Reply New Discussion
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:
# -*- 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 x
if __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
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:
CODE
#!/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 x
if __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
Tue Jan 29, 2013 Reply New Discussion
thanks MC, that worked too
wow another Kiwi in the Forum.... Kiora bro!
Tue Jan 29, 2013 Reply New Discussion
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
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
Tue Jan 29, 2013 Reply New Discussion
awesome... by the way I'm from Wellington... good to see you back here bud
Tue Jan 29, 2013 Reply New Discussion
Simply Bzip2'ing A File? Should be simple, but not quite so :) (5)
|
(20) Learnin Python -- Where To Start?
|
Index




