GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 17-Jan-2009, 11:43
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


Quote:
I Am New To Python As Well
Did YOU write this program? If so you may have aimed a little high! But you can probably get it fixed up IF you can be patient and establish good troubleshooting techniques.

When you don't understand certain behaviors, try doing small command line exercises to learn about them as I tried to demonstrate in the previous posts. Did you play with the array like I showed??? It shows exactly how your error is occurring. Take a few minutes to do things like that.

Another thing is to print the code out on paper , take a pencil and step through the program by hand. Write down and update variable values as they are declared and are changed. Go slowly and follow program flow from start to end.

Another thing is to add extra temporary 'print' lines in the program to report values to you as the program progresses. Even something like:
print " In loop 1"
can be very helpful to see where you are in the program when a problem occurs. You can even declare a temporary variable to count loop iterations.

So anyhow I'll try again.
I know the error message is a bit cryptic but if I break it down I begin to see that:
on line 20 (in the function showhand() (which was last called from line 33) )
there is the following problem with this line:
print "the computer is showing a %d" % computer[0]

IndexError: list index out of range.

As I tried to explain above the 'list' (array) it is referring to is named 'computer'.
You are trying to print computer[0] ...the first element of the list.

The problem is that in the program you redefine 'computer' with lines like this:
computer = []

That declaration says that 'computer' is an 'empty list' which means that is is a name only and has no data associated with it. So... THERE IS NO FIRST ELEMENT and with the print line you are trying to access something that doesn't exist.
In other words: IT IS NO MORE! Hence the error.

So if you want to print computer[0] you need to make sure there is a computer[0].
Change all occurances of 'computer = []' at least to 'computer = [0]' (zero as a first element)
(hint: same for 'player')

So make those changes and let's see where the next problem is.
Post your new code with the next problem.
Last edited by Howard_L : 17-Jan-2009 at 12:55.
  #12  
Old 17-Jan-2009, 21:20
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Black jack game


In general, the "list index out of range" means that the index of your list (the value at a particular position within the list) is not available, i.e. it is out of the scope ("range") that the list has been set for.

For example, if you created a list that has 5 values in it but you somehow coded it to return the value at the 8th position, it will give you an error.

I'll admit, I haven't fully looked over your code closely but it looks like the function showHand() is trying to print an integer that doesn't exist, either because the list is empty or because you're calling explicitly for an index that isn't there.

You do have the list computer being initialized many different times so there's a good chance that it's being reinitialized then called, thus there won't be a value present and giving you the error.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #13  
Old 18-Jan-2009, 10:15
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Re: Black jack game


ive made it work.....thank you
  #14  
Old 18-Jan-2009, 10:23
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Re: Black jack game


do you kno how to do loops
im struggling with this loop
"a borough has made the following prognosis for the changes in population over the next few years:
1) at the start of 2004 there was 26 000 inhabits
2)the rate of birth and death are 0.7 and 0.6 of the population
3) people moving in and ut are 30 and 325
i have to write a program to calculate the boroughs estimated population at the begginin of a particular year.. the year should be requested for user..
here is what i wrote

year = input ("please enter year: ")

Birtrate = 0.7
Deathrate = 0.6
movingin = 300
movingout = 325

Population = 2600

for x in range(2004,year)
pop = pop + (pop * 0.1) -(325-300)
print pop

but it keeps givin me d sam digit..do you kno were am goin wrong
  #15  
Old 18-Jan-2009, 18:54
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


Well that wouldn't even run for me. I got errors:
Code:
C:\py>population-01.py File "C:\py\population-01.py", line 11 for x in range(2004,year)
You need a colon at the end of the 'for' statement and indent for scope of the loop.
Code:
C:\py>population-01.py Traceback (most recent call last): File "C:\py\population-01.py", line 12, in <module> pop = pop + (pop * 0.1) -(325-300) NameError: name 'pop' is not defined
You also need to declare pop somewhere before the big calculation line because the math on the right (which contains the name 'pop' which at that point unidentified) will be done before the 'pop =' part gets evaluated. (that's operator precedence)
Python Code:
#year = input ("please enter year: ")
year = 2008

Birtrate = 0.7
Deathrate = 0.6
movingin = 300
movingout = 325
pop = 0;

Population = 2600

for x in range(2004,year):
  pop = pop + (pop * 0.1) -(325-300)
  print pop
Code:
C:\hlenderk\py>population-01.py -25.0 -52.5 -82.75 -116.025
That population is not doing too well...
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with a simple "dragon" game. ddaigle C++ Forum 13 04-Dec-2007 07:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.