Page 1 of 1

Python coders

Posted: Fri Apr 08, 2005 11:05 am
by Nightshade
Can anyone tell me why this goofy program won't work?

Code: Select all

#Craps_Game

import random

random.seed()

def roll_dice():
    r1 = random.random()
    Die_1 = int(6 * r1) +1
    r2 = random.random()
    Die_2 = int(6 * r2) + 1
    Total = Die_1 + Die_2
    return(Total)

roll_dice()
Total_1 = Total
if(Total_1 == 7) or (Total_1 == 11):
    print "Lucky ", Total_1, "! You win!"
elif(Total_1 == 2) or (Total_1 == 3) or (Total_1 == 12):
    print Total_1, "CRAPS! You lose!"
else:
    roll_dice()
    Total_2 = Total
    roll_count = 0
    while(roll_count < 20):
        if(Total_2 == Total_1):
            roll_dice()
            Total_2 = Total
        elif(Total_2 == 7):
            print "Lucky ", Total_2, "! You win!"
It keeps telling me that Total isn't defined.

Posted: Fri Apr 08, 2005 11:12 am
by SOAPboy
Wheres the rest..
*disclamer i know nothing mroe than VB,HTML,*

Shouldnt the Roll_dice call your Random ints?

or maybe im just missing that somewhere..

O_o

and god, that looks increadibly simple as far as the language goes.. might have to pick up a python book..

Posted: Fri Apr 08, 2005 11:16 am
by SOAPboy
Erm.. wait, im wrong..

*ponders a little more*

Posted: Fri Apr 08, 2005 11:18 am
by Nightshade
random.seed() established a pointer in a table of randomly generated numbers. It's just part of a random number generation routine to simulate rolling two dice.

Posted: Fri Apr 08, 2005 11:21 am
by SOAPboy
yeah i grabbed python, im tinkering.. been a while since ive touched code tho.. gimme a few, maybe ill get it working lol

Posted: Fri Apr 08, 2005 11:28 am
by SOAPboy
ATM, the only thing i can think of, is its wanting TOTAL to be defined by itself O_o

hmm....

Posted: Fri Apr 08, 2005 11:45 am
by SOAPboy
Any luck?

"kinda" catching onto this.. Dunno why i keep thinking it all should be pre defined.. god damn VB teacher.. >:E

Posted: Fri Apr 08, 2005 11:56 am
by SOAPboy
K.. a little more tinkering..

If you just add a number to Total_1 = itll come up saying whatever, problem is, Total (from dice_roll) isnt defined.. Define it..

I know HOW it needs to be fixed, i just dont "know" the code.. sigh, im gonna learn this shit now. lmfao.. 7am and im learning a new language.. good times.. thanks Q3W

Posted: Fri Apr 08, 2005 12:15 pm
by Doombrain
fucking loser

Posted: Fri Apr 08, 2005 12:27 pm
by SOAPboy
hey nightshade.. thanks for this thread.. think i might really enjoy python.. ill keep fucking with this craps thing after work, post or PM me if you get it working before i do, id like to know the solution..

Posted: Fri Apr 08, 2005 12:29 pm
by Doombrain
BE MY FRIEND!

Posted: Fri Apr 08, 2005 12:30 pm
by SOAPboy
Doombrain wrote:BE MY FRIEND!
Stop spamming everyones threads.. its old, and unfunny

Re: Python coders

Posted: Fri Apr 08, 2005 12:46 pm
by phoq
Nightshade wrote:Can anyone tell me why this goofy program won't work?

Code: Select all

#Craps_Game

import random

random.seed()

def roll_dice():
    r1 = random.random()
    Die_1 = int(6 * r1) +1
    r2 = random.random()
    Die_2 = int(6 * r2) + 1
    Total = Die_1 + Die_2
    return(Total)

Total = roll_dice()
Total_1 = Total
if(Total_1 == 7) or (Total_1 == 11):
    print "Lucky ", Total_1, "! You win!"
elif(Total_1 == 2) or (Total_1 == 3) or (Total_1 == 12):
    print Total_1, "CRAPS! You lose!"
else:
    Total = roll_dice()
    Total_2 = Total
    roll_count = 0
    while(roll_count < 20):
        if(Total_2 == Total_1):
            Total = roll_dice()
            Total_2 = Total
        elif(Total_2 == 7):
            print "Lucky ", Total_2, "! You win!"
It keeps telling me that Total isn't defined.
Well it's right.
The function roll_dice returns a value (number, whatever) that you need to assign to the variable Total.
Like this:

Total = roll_dice()

What you're doing is simply calling the function (which returns a value) but you're not doing anything with it (you're not storing it in a variable). The definition of the variable Total in the function roll_dice is not public, so it's expected that the bottom part doesn't know it.

edit: fixed the code (with all three calls to roll_dice()) in the quote, I'm not going to bother with the rest but if you need any help just ask.

Posted: Fri Apr 08, 2005 12:52 pm
by phoq
Seems to work now, just downloaded python and ran the program.
Tip though: you can assign the value of roll_dice directly to total_1, total_2 and total_3 instead of assigning it to Total and then to the other 3.


Just out of interest: what are you using python for? Educational purpose? (never heard of it, actually had to google it to find out what it is).

Posted: Fri Apr 08, 2005 12:59 pm
by PhoeniX
>:E i was going to suggest the function thing.

The code is awful; you have to indent to create functions etc. C++ is much nicer structered with the braces { }

Re: Python coders

Posted: Fri Apr 08, 2005 2:35 pm
by Nightshade
phoq wrote: Well it's right.
The function roll_dice returns a value (number, whatever) that you need to assign to the variable Total.
Like this:

Total = roll_dice()

What you're doing is simply calling the function (which returns a value) but you're not doing anything with it (you're not storing it in a variable). The definition of the variable Total in the function roll_dice is not public, so it's expected that the bottom part doesn't know it.

edit: fixed the code (with all three calls to roll_dice()) in the quote, I'm not going to bother with the rest but if you need any help just ask.
Yeah, I had the call fucked up, which I suspected from the beginning. Thanks.

Hey GothBoi, glad you like Python, hope it makes up for me shooting your dad.

Posted: Fri Apr 08, 2005 4:42 pm
by duffman91
PhoeniX wrote:>:E i was going to suggest the function thing.

The code is awful; you have to indent to create functions etc. C++ is much nicer structered with the braces { }
Python is an interpreted language, not compiled. Different ball for a different game.