Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Data
#1
I haven't coded a thing in 25 years and that was mostly basic. I don't really understand what the various shells are for. And I'm having issues with data flow (where the data goes and how to access it.)


In the example below I don't understand how to access the data that I return from the function after I drop out of the function.

# Player Generator

from random import randrange


def heightdice(die):
        age=randrange(15,19)
        htroll=[]
        b=0
        while b<die:		
                roll=randrange(0,13)
                htroll.append(roll)
                b=b+1
        htroll.sort()
        if htroll[1]>0:
                if htroll[0]==htroll[1]:
                        htroll[0]=htroll[0]-1
                        htroll[1]=htroll[1]+1
        for w in htroll:
                print (w,end=' ')
        print()
        print(sum(htroll)+130)
        ageheight=[]

        ageheight.append(sum(htroll[3:10])+int(htroll[2]/2)+129)
        ageheight.append(sum(htroll[2:10])+130)
        ageheight.append(ageheight[1]+int(round(htroll[1]/2+.1,0)))
        ageheight.append(ageheight[1]+htroll[1])
        ageheight.append(ageheight[3]+int(round(htroll[0]/2+.1,0)))
        ageheight.append(ageheight[3]+htroll[0])
        height=ageheight[age-15]
        print('Age:',age,' Height:',height)
        return ageheight,age


heightdice(10)
if ageheight[age-15]<135+age*2:
        heightdice(10)
Error:
So how do I make ageheight and age usable Traceback (most recent call last): File "/Users/raymond/Documents/playgen2.py", line 37, in <module> if ageheight[age-15]<135+age*2: NameError: name 'ageheight' is not defined
Reply
#2
You're throwing the returned values away.
You should assign them to a variable if you want to continue using them.
>>> def something():
...     return 1, 2
...
>>> a, b = something()
>>> a
1
>>> b
2
Reply
#3
Hello and welcome!
You can use the returned object asign it to a variable:

>>> def foo():
...     message = input('> ')
...     return message, len(message)
...
>>> msg, msg_len = foo() # here is happening
> Hello, Bob! How are you?
>>> print('({}): {}'.format(msg_len, msg))
Output:
(24): Hello, Bob! How are you?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thanks, I'm back in business.

This may be nit picking but my OCD is kicking in. After I drop out of the heightdice function I enter a while loop to see if the height is within parameters.

heightdice(10)
ageheight,age=heightdice(10)

academylvl=randrange(1,9)
leaguelvl=randrange(1,10)
while ageheight[age-15]<135+age*2+int((academylvl+leaguelvl)/2-.1):
        heightdice(10)
        ageheight,age=heightdice(10)
Even if the height is in parameters, the function heightdice is always called a second time. In this case the second "roll" was out of parameters so I had to enter the function again. The third output set was also easily within parameters so the fourth time through the function was also redundant. Ultimately I'm getting the output I want, its just sloppy in the execution.

Output:
1 4 4 5 6 7 7 9 9 11 193 Age: 17 Height: 190 0 0 0 1 1 3 3 4 5 6 153 Age: 16 Height: 153 0 2 2 3 6 7 7 8 9 11 185 Age: 16 Height: 183 0 2 4 5 6 7 8 8 12 12 194 Age: 18 Height: 194
Reply
#5
(Sep-10-2017, 06:47 PM)RMcGovern Wrote: Even if the height is in parameters, the function heightdice is always called a second time.
Of course it is, you 're calling the function two times in a row, discarding the return values of the first call:
heightdice(10)
ageheight,age=heightdice(10)
Reply
#6
Thanks It's fixed. I feel stupid now hopefully that passes.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020