Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List out of scope?
#1
Hello, I am at my wits end trying to solve this problem. When I try to execute my program, the assembler complains that
total = scores [0] + scores[1] + scores[2]
 is out of scope. I imagine that
del scores[0]
 is somehow getting repeated too many times but I don't see how.

The purpose of this part of the program is to "roll" 4 six-sided dice, discard the lowest score and sum the remaining 3 scores, repeating this process 6 times.

I'm certain there are better ways to accomplish what I'm trying to do but I'm still learning the language.

def generateAbilityScores():
i = 0
j = 0
scores = []
pool = []

while i < 6:
while j < 4:
roll = rollDsix()
scores.append(roll)
j += 1 

scores.sort()
del scores[0]
total = scores[0] + scores[1] + scores[2]
pool.append(total)
i += 1
Reply
#2
Please ensure that the code you're posting reflects the indentation that reproduces your problem.
Reply
#3
Sorry, the post formatting messed it up after previewing the post.

def generateAbilityScores():
	i = 0
	j = 0
	scores = []
	pool = []

	while i < 6:
		while j < 4:
			roll = rollDsix()
			scores.append(roll)
			j += 1		

		scores.sort()
		del scores[0]
		total = scores[0] + scores[1] + scores[2]
		pool.append(total)
		i += 1
Reply
#4
where is the function definition for roll = rollDsix()?
Reply
#5
(Nov-07-2016, 07:52 PM)Larz60+ Wrote: where is the function definition for roll = rollDsix()?

def rollDsix():
	number = random.randint(1, 6)
	return number
Wish I could edit my original post to reflect this stuff instead of reposting. :(
Reply
#6
You need to supply working snippets - missing imports
You cannot assume we know what they are

I think if you put print statements (as follows) in your code,
the error will stand out for you:
import random


def rollDsix():
    number = random.randint(1, 6)
    return number

def generateAbilityScores():
    i = 0
    j = 0
    scores = []
    pool = []

    while i < 6:
        while j < 4:
            roll = rollDsix()
            print('roll: {} scores: {}'.format(roll, scores))
            scores.append(roll)
            j += 1

        scores.sort()
        del scores[0]
        print('after delete scores: {}'.format(scores))
        total = scores[0] + scores[1] + scores[2]
        pool.append(total)
        i += 1

generateAbilityScores()
results:
Output:
roll: 2 scores: [] roll: 2 scores: [2] roll: 6 scores: [2, 2] roll: 1 scores: [2, 2, 6] after delete scores: [2, 2, 6] after delete scores: [2, 6] Traceback (most recent call last):   File "M:/python/m-p/p/play/nisc11.py", line 29, in <module>     generateAbilityScores()   File "M:/python/m-p/p/play/nisc11.py", line 25, in generateAbilityScores     total = scores[0] + scores[1] + scores[2] IndexError: list index out of range
Reply
#7
OK, from the top, here's all the relevant stuff.

import random

def rollDsix():
	number = random.randint(1, 6)
	return number

def generateAbilityScores():
	i = 0
	j = 0
	scores = []
	pool = []

	while i < 6:
		while j < 4:
			roll = rollDsix()
			scores.append(roll)
			j += 1		

		scores.sort()
		del scores[0]
		total = scores[0] + scores[1] + scores[2]
		pool.append(total)
		i += 1
Again, this section is supposed to "roll" 4 six-sided dice, remove the lowest score and add up the remaining 3 values and store that value. I want it to repeat this process 6 times.
Reply
#8
we posted simultaneously see above
Reply
#9
Oh! I see.

Like I said, I'm still learning the language and didn't know how to print like that. Thanks for the demonstration. I should be able to work it out from here.

By the way, when are new accounts allowed to edit their posts?
Reply
#10
Editing and deleting threads/posts
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,689 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 838 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,830 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,533 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 3,034 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,800 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Block of code, scope of variables and surprising exception arbiel 8 3,405 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,485 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Solving a scope issue profconn1 4 2,564 Nov-01-2019, 07:46 PM
Last Post: profconn1
  Namespace and scope difference Uchikago 9 4,583 Jul-03-2019, 03:36 PM
Last Post: Uchikago

Forum Jump:

User Panel Messages

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