Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with variables
So yeah, I'm trying to make a simple randomized generator in Python for my own-design board game but I found a problem. To be more specific, I want to generate a list of items that the player will gain by a specific rule (i don't want to go deeper into the rules Dodgy ). Now the problem is, that sometimes (depending on what generates), Python screams at my face that:
Traceback (most recent call last): File "", line 358, in <module> print(count, end=' ') NameError: name 'count' is not defined
Now, when you'll browse the code i'm putting below, you'll notice that the variable 'count' IS defined. It's probably a "missclick" of mine or sth like that but I have no idea where and how does it look. Can you help me?

650 lines of code with no functions, just a huge string of if/elif/else? No wonder you're having trouble finding the bugs. Please, learn functions, learn how to use data for conditional processing.

As to your error, count is not necessarily defined. If x is 91 or less, you go to an if/elif with no else. If neither of those conditions are true, count won't be defined.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Thanks, I get it now.

BTW I tried functions and it looks like I need more time. Welp, at least this is fixed Tongue
First, I echo ichabod801's comments.

Second, here's my crack at debugging your code: I found that if I use a stone hoe without silk touch, and a fortune enhancement of 21711917, that puts me in the apparently offending section of the code. If we look at the last five lines (358 through 362), we have this:

                count = 1
                item = "cornflower"
            print(count, end=' ')
            r = r - 1
"count" is defined, but only under the "else" that's started on line 357. The two "print" commands are not on the same indentation, so they're actually in the "while" loop that starts on line 274 -- they'll get executed every time in that while loop regardless of any of the checks (and subsequent settings of "count") between line 276 and 359. This is what makes it possible to get to line 360 without defining "count". I suspect what you want is this:

                count = 1
                item = "cornflower"
                print(count, end=' ')
                r = r - 1
... which puts those print statements under the "else" where they (apparently) belong. This seems to avoid generating the "count not defined" error under the same conditions (The stone hoe without silk touch, and a fortune enhancement of 21711917). However, I can't say that's a definitive test of all of the possibilities.

I'm assuming you're not done with the program yet, because when I go out with a bucket I always get nothing.

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem using exec to save local variables dkarl 0 388 Dec-01-2019, 08:52 AM
Last Post: dkarl
  A problem with defining variables meru120 5 1,010 Apr-16-2019, 03:13 PM
Last Post: buran
  Problem with variables Steffenwolt 4 1,195 Jul-14-2018, 07:27 PM
Last Post: Steffenwolt
  [split] Problem using global variables RedSkeleton007 1 1,202 Nov-10-2017, 09:13 PM
Last Post: sparkz_alot
  Vending Machine Program: Problem with variables icabero0225 2 5,297 Jun-08-2017, 11:04 AM
Last Post: buran
  MySQLdb, problem with query with user-defined variables buran 6 3,041 Feb-03-2017, 06:16 PM
Last Post: buran

Forum Jump:

User Panel Messages

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