[Tutor] Help with class example

2012-10-26 Thread Frank Pontius
Hello,
I'm taking a beginners course on Python.

Have class example which is to be used in H/W, which I can't get to work.
Thus I figure the example is wrong.  But I need this to get H/W done.

Using 'import random' and random.randrange in function, returning #.

import random

def RandomNumberGen ():
if random.randrange(0, 2) == 0:
 X  return "tails"
return "heads"


Simple enough, but running in my Python 2.7.3 IDLE - it will not even run,
and give syntax (red char box) error at the X above.

Frank

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] why different result from two similar ways

2012-11-05 Thread Frank Pontius
Hello,
I have code that works.  Then tried to move some of it into function
IncrementAndRebuildInput, then result changes, I no longer have same result
as when code in function was inline - why?

(Function version way below)

Inline version: (this correctly adds 1 to all numbers in text string,
and prints it out with incremented #s):

def IsNum(string):
#print "IsNum string", string
for char in string: #checks string groupings to be all nums
if not char.isdigit():
#print "false"
return False
#print "true"
return True


def main():
text = raw_input("Type something: ")
print
if text:
print text
else:
text = "I got 432 when I counted, but Jim got 433 which is a lot for
only 6 cats, or were there 12 cats?"
print text  #string input

SplitText = text.split()#makes a list from string input
#   print SplitText
#   print "Did I print LIST?"
for index, element in enumerate(SplitText):
if IsNum(element):  #looks@every list element,
checks for #
num = int(element) + 1  #if #, increments it
#   print num
#   print "Bkpt8"
SplitText[index] = str(num)
else:
pass
#print "bkpt9"

 #   NewString = " ".join(SplitText)
print "bkpt10"
print
print SplitText
print
print " ".join(SplitText)
print
print "END"
main()

OUTPUT:::
>>>
>>> 
Type something: 

I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?
bkpt10

['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '13', 'cats?']

I got 433 when I counted, but Jim got 434 which is a lot for only 7 cats, or
were there 13 cats?

END
>>>


Function version way below: (This version does not produce the
same output (w/numbers incremented by 1)

def IsNum(string):
#print "IsNum string", string
for char in string: #checks string groupings to be all nums
if not char.isdigit():
#print "false"
return False
#print "true"
return True



def IncrementAndRebuildInput(text):
newtext = text.split()#makes a list from string input
print newtext
#   print "Did I print LIST?"
for index, element in enumerate(newtext):
if IsNum(element):  #looks@every list element,
checks for #
num = int(element) + 1  #if #, increments it
print num
#   print "Bkpt8"
newtext[index] = str(num)
print newtext
print "NOWHERE"
else:
pass
#print "bkpt9"
print newtext # contains new list w/#'s incremented by 1
print "Point6"
return newtext


def main():
text = raw_input("Type something: ")
print
if text:
print text
else:
text = "I got 432 when I counted, but Jim got 433 which is a lot for
only 6 cats, or were there 12 cats?"
print text  #string input

IncrementAndRebuildInput(text)

#   print "bkpt10"
print
print text#  **  Placing previous inline
code into function changes result - what am I doing wrong?**
print "Point7"
print "".join(text)
print
print "END"
main()

OUTPUT:::
>>>
>>> 
Type something: 

I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?
['I', 'got', '432', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '433',
'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were',
'there', '12', 'cats?']
433
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '433',
'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were',
'there', '12', 'cats?']
NOWHERE
434
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were',
'there', '12', 'cats?']
NOWHERE
7
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '12', 'cats?']
NOWHERE
13
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '13', 'cats?']
NOWHERE
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '13', 'cats?']
Point6

I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?
Point7
I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?

END
>>> 
>>>