Python coders

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
Nightshade
Posts: 17020
Joined: Fri Dec 01, 2000 8:00 am

Python coders

Post 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.
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post 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..
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post by SOAPboy »

Erm.. wait, im wrong..

*ponders a little more*
[size=75][i]I once had a glass of milk.

It curdled, and then I couldn't drink it. So I mixed it with some water, and it was alright again.

I am now sick.
[/i][/size]
[img]http://img162.imageshack.us/img162/3631/171164665735hk8.png[/img]
Nightshade
Posts: 17020
Joined: Fri Dec 01, 2000 8:00 am

Post 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.
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post 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
[size=75][i]I once had a glass of milk.

It curdled, and then I couldn't drink it. So I mixed it with some water, and it was alright again.

I am now sick.
[/i][/size]
[img]http://img162.imageshack.us/img162/3631/171164665735hk8.png[/img]
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post by SOAPboy »

ATM, the only thing i can think of, is its wanting TOTAL to be defined by itself O_o

hmm....
[size=75][i]I once had a glass of milk.

It curdled, and then I couldn't drink it. So I mixed it with some water, and it was alright again.

I am now sick.
[/i][/size]
[img]http://img162.imageshack.us/img162/3631/171164665735hk8.png[/img]
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post by SOAPboy »

Any luck?

"kinda" catching onto this.. Dunno why i keep thinking it all should be pre defined.. god damn VB teacher.. >:E
[size=75][i]I once had a glass of milk.

It curdled, and then I couldn't drink it. So I mixed it with some water, and it was alright again.

I am now sick.
[/i][/size]
[img]http://img162.imageshack.us/img162/3631/171164665735hk8.png[/img]
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post 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
[size=75][i]I once had a glass of milk.

It curdled, and then I couldn't drink it. So I mixed it with some water, and it was alright again.

I am now sick.
[/i][/size]
[img]http://img162.imageshack.us/img162/3631/171164665735hk8.png[/img]
Doombrain
Posts: 23227
Joined: Sat Aug 12, 2000 7:00 am

Post by Doombrain »

fucking loser
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post 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..
[size=75][i]I once had a glass of milk.

It curdled, and then I couldn't drink it. So I mixed it with some water, and it was alright again.

I am now sick.
[/i][/size]
[img]http://img162.imageshack.us/img162/3631/171164665735hk8.png[/img]
Doombrain
Posts: 23227
Joined: Sat Aug 12, 2000 7:00 am

Post by Doombrain »

BE MY FRIEND!
SOAPboy
Posts: 8268
Joined: Sun Apr 13, 2003 7:00 am

Post by SOAPboy »

Doombrain wrote:BE MY FRIEND!
Stop spamming everyones threads.. its old, and unfunny
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

Re: Python coders

Post 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.
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

Post 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).
Last edited by phoq on Fri Apr 08, 2005 1:05 pm, edited 1 time in total.
User avatar
PhoeniX
Posts: 4067
Joined: Fri Aug 04, 2000 7:00 am

Post 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 { }
Nightshade
Posts: 17020
Joined: Fri Dec 01, 2000 8:00 am

Re: Python coders

Post 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.
User avatar
duffman91
Posts: 1278
Joined: Thu Jan 25, 2001 8:00 am

Post 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.
Post Reply