[Tutor] Stuck on error
Felix Dietrich
felix.dietrich at sperrhaken.name
Sun Dec 22 07:04:12 CET 2013
"NZHacker1 ." <nikolausmz at gmail.com> writes:
> import random
>
> for i in range(1):
> RN1 = random.randint(1,75)
There is no point of using a for loop if you only intent to do something
once. But looking at the following lines, it actually seems like you
want something to happen multiple times:
> for i in range(1):
> RN2 = random.randint(1,75)
>
> [...]
What you could have done here is to use a `list'. Like so:
random_numbers = []
for i in range(5):
random_numbers.append(random.randint(1,75))
You can then index into the list to get the individual elements. Indices
start at 0. e.g.:
random_numbers[0]
returns the first random number (RN1). The last element (the 5th) in
this example has index 4.
> [...]
> z = 0
> Game = ('Game')
> [...]
> z = z + 1
> [...]
> Game + z = N1 + N2 + N3 + N4 + N5 + MB
> z = 0
Here is the exception again:
> There's an error in your program:
> ***Cant assign to operator.(Mega Millions, line 47)
It says that you cannot have the `+' operator on the left side of an
assignment (that is the equal sign).
Game = N1 + N2 + N3 + N4 + N5 + MB
should work, though it might not do what you want it to. Game was
initially a reference to a `tuple' holding a string:
Game = ('Game')
and is now an integer:
Now, I am not sure want your intentions with that line are (try to add
that when you ask a question, like: I have a problem with my program, it
does X (throws an exception, breaks, crashes) and I want it to do Y). I
am guessing that you want variables `Game0', `Game1', ..., `Gamez' to
exist after it? In that case a list is, again, what you want:
game = []
game.append(N1 + N2 + N3 + N4 + N5 + MB)
Now game is a list and, as before, you can access the individual
elements by indexing:
game[2]
returns the result of the third game. If there has not yet been a third
game you will get an `IndexError'. The number of games already played
is the length of the list and can be obtained with:
len(game)
Here are the links to the tutorial section for lists:
python2: http://docs.python.org/2.7/tutorial/introduction.html#lists
python3: http://docs.python.org/3/tutorial/introduction.html#lists
> while plays != 0:
> Plays = Plays - 1
You are testing lowercase `plays' in the while loop's condition here but
are using uppercase `Plays' in the loop's body.
> z = z + 1
> print(Game + str(z))
With a list it can look like this:
for g in games:
print g
> Line 47 in highlighted in red.
It is nice that you marked the line, but people prefer plain-text
messages on this list/group (html messages screw-up the code's
formatting, and for some their reader doesn't display the message at
all). You can use comments with some fancy ascii symbols around the
offending line.
A few notes regarding the choice of your variable names:
- all-uppercase variable names (FOO) should be reserved for
"constants",
- first letter capitalisation should be reserved for (something called)
classes,
- give them meaningful names
Here's some reading about the suggested python-code styling conventions
(you don't need to read that now):
http://www.python.org/dev/peps/pep-0008/
> I'm not finished with the program
Happy hacking.
--
Felix Dietrich
More information about the Tutor
mailing list