![]() |
|
#11
|
|||
|
|||
Re: Black jack gameQuote:
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
|
||||
|
||||
Re: Black jack gameIn 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
|
|||
|
|||
Re: Black jack gameive made it work.....thank you
|
|
#14
|
|||
|
|||
Re: Black jack gamedo 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
|
|||
|
|||
Re: Black jack gameWell that wouldn't even run for me. I got errors:
Code:
Code:
Python Code:
Code:
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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