Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (19 June 2013 - 02:28 PM) Long Life To Asta New Era
@  agyat : (19 June 2013 - 01:58 PM) New Era Start At Asta Or Asta Start In New Era. :unsure:
@  yordan : (16 June 2013 - 05:41 PM) You're Welcome, Agyat!
@  agyat : (16 June 2013 - 07:38 AM) Thanks Yordan...
@  velma : (16 June 2013 - 12:06 AM) I Have Asked Opa To Check For A Backup.. He'll Let Me Know Soon :)
@  velma : (16 June 2013 - 12:05 AM) T_T It Seems That Someone Has Deleted That Topic Since I Found The Url Of The Topic But It Gives Me An Error
@  yordan : (15 June 2013 - 10:31 PM) @velma : It's A Tuto On How To Create A Login Program.
@  yordan : (15 June 2013 - 10:31 PM) Happy Birthday To Youuuuuu Agyat!
@  yordan : (15 June 2013 - 10:31 PM) Ba$
@  agyat : (15 June 2013 - 04:41 PM) :(
@  agyat : (15 June 2013 - 04:41 PM) Where The Hall I Were? 15Th Is Almost At End And No-One Wished Me "happy Birthday"!!!
@  velma : (14 June 2013 - 10:39 AM) Which Tutorial Is He Searching For?
@  velma : (14 June 2013 - 10:38 AM) Which Tutorial Is He Searching For?
@  yordan : (14 June 2013 - 07:47 AM) Ok, Have A Look Tomorrow.
@  yordan : (13 June 2013 - 03:19 PM) @velma, Can You Have A Look At Feelay's Problem? Seems That His Tutorial Is Not Searchable Today.
@  Feelay : (13 June 2013 - 08:11 AM) Oh, Haha
@  velma : (12 June 2013 - 05:39 PM) T_T Lately My Levels Of Procrastination..... **sigh**
@  velma : (12 June 2013 - 05:38 PM) I'll Do It Later
@  velma : (12 June 2013 - 05:38 PM) Procrastinators.. People Who Keep Saying "i'll Do This In A Bit"
@  Feelay : (12 June 2013 - 02:05 PM) Deal Punishments To What?

Replying to The Return Statement


Post Options

    • Can't make it out? Click here to generate a new image

  or Cancel


Topic Summary

manuleka

Posted 29 January 2013 - 09:17 PM

awesome... by the way I'm from Wellington... good to see you back here bud

mastercomputers

Posted 29 January 2013 - 09:40 AM

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

manuleka

Posted 29 January 2013 - 08:28 AM

thanks MC, that worked too :) wow another Kiwi in the Forum.... Kiora bro!

mastercomputers

Posted 29 January 2013 - 06:58 AM

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 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

manuleka

Posted 22 September 2012 - 08:35 PM

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()

manuleka

Posted 20 August 2012 - 04:49 AM

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
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?

Review the complete topic (launches new window)