LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2003, 04:26 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
python: return not returning


Hello,

I am trying to write a little program in python. It manipulates strings according to rules of a formal system. I have written each rule in the form of a function. The function works fine, it takes the input string, and changes it if it fits the rules. The problem is that the function will not let me access the new string outside of the function. I thought that was what the "return" did?

When I try to access the changed string outside of the function, it just uses the value of the string before it went into the function...

How do I "export" the changed string, so I can use it outside of the function that changed it? I sort of need to keep the variable name the same throughout the program. Here is the code for your bemusement. Keep in mind I am an absolute coding noob please
Code:
# first we set some variables

m = "M"
i = "I"
u = "U"
start_string = m + i # the string you begin with according to the book

print "You begin with", start_string # just because ;)
print
string = start_string
print "The Rules:"
print "Rule I: if string ends with 'I', add 'U'"
print "Rule II: if string is 'Mx', make it 'Mxx'"
print "Rule III: if 'III' occurs, make it 'U'"
print "Rule IV: if 'UU' occurs, drop it"
print

# start defining rules
# RULE I, if string ends with "I", add "U"
def rule_I(string):
   x_string = string
   char_string = x_string[-1]
   if char_string == i:
      x_string = x_string + u
      string = x_string
      print "Rule I: string is now:", string #prints "MIU" as it should...
      return string # this does not appear to do anything
   else:
      print "Rule I does not apply here"

# run the original "MI" through the rules
print "First level iteration"
print
rule_I(string)
print string #this prints "MI", the string before it went into rule_I()
TIA
PS: i have ommited the other rules here, I want to get the first one working first...

Last edited by bulliver; 01-09-2003 at 04:28 PM.
 
Old 01-09-2003, 06:48 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
OK, well if anyone cares, I thought I would take a stab in the dark and use "global" within the function a la php, and what do you know, it worked...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
installing python library's (Python Numeric) Four Linux - Newbie 1 10-16-2005 02:31 PM
WineX, python-gnome, and python-gtk DrD Fedora 0 08-03-2004 12:11 PM
install python 2.3 ,necssary to remove python 2.2 ngan_yine Linux - Newbie 7 12-28-2003 04:07 PM
Try Python, O'reilly Learning Python haknot Programming 5 02-15-2002 08:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration