Python Forum
NameError: name is not defined
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name is not defined
#1
I'm really new to python. Can someone help me figure out what's wrong with my crappy game? It says NameError: name 'DSC' is not defined on line 55 in main.py
import time
import os

name = raw_input("""Hello, welcome to Snek World.
You are a snek what is your name? """)
time.sleep(1)
print 'Hi, %s Welcome to Snek World' % name
time.sleep(2)

# Pick a Biome

print """Here in Snek World we have tons of
different biomes! Pick where you want to live. (Sneks vary in each biome.)
1. Desert
2. Rainforest
3. Plains
"""
biome = int(raw_input())
time.sleep(2)
os.system('cls')

# Desert Snakes (DSC = Desert snake choice)

if biome == 1:
    DSC = raw_input("""Good choice! There are lots of sneks from the
desert! (Pick which snek you want to be)
1. Kenyan Sand Boa
2. Cobra
3. California King Snake
""")

# Rainforest snakes (RSC Rainforest snek choice)

elif biome == 2:
    RSC = input("""Good choice! There are lots of sneks from the
rainforest! (Pick which snek you want to be)
1. Tri-color hognose
2. Rainbow Boa
3. Bush Viper
""")

# Plains (PSC Plains snek choice)

elif biome == 3:
    PSC = input("""Good choice! There are lots of sneks from the
plains! (Pick which snek you want to be)
1. Hognose snake
2. Diamondback rattlesnake
3. Rat snake
""")

# DSC

if DSC == 1:
    print '%s the Kenyan sand boa, I like it!' % name
if DSC == 2:
    print '%s the Cobra, I like it!' % name
if DSC == 3:
    print '%s the California king snake, I like it!' % name

# RSC

if RSC == 1:
    print '%s the Tri-color hognose, I like it!' % name
if RSC == 2:
    print '%s the Rainbow boa, I like it!' % name
if RSC == 3:
    print '%s the Bush viper, I like it!' % name

# PSC

if PSC == 1:
    print '%s the Hognose snake, I like it!' % name
if PSC == 2:
    print '%s the Diamondback rattlesnake, I like it!' % name
if PSC == 3:
    print '%s the Rat snake, I like it!' % name
Reply
#2
if biome is not 1, when it reach line 54 your script doesn't know what DSC is and what the value is..
Reply
#3
(Sep-25-2017, 08:03 PM)buran Wrote: if biome is not 1, when it reach line 54 your script doesn't know what DSC is and what the value is..
So what should I do?
Reply
#4
refactor your code. look at https://python-forum.io/Thread-Text-Adventure-Tutorial
Reply
#5
(Sep-25-2017, 08:08 PM)buran Wrote: refactor your code.

How do I do that?

(Sep-25-2017, 08:08 PM)buran Wrote: refactor your code. look at https://python-forum.io/Thread-Text-Adventure-Tutorial

What is refactoring? How would it fix my code?
Reply
#6
Refactoring is basically reorganizing your code. The problem is the if/elif/else structure of your code. If you structured your code differently, you wouldn't run into the problems you are having. You could fix your code without restructuring it, by defining dummy values for DSC and the other similar variables at the beginning. But in the long run that structure would lead to other problems as you expanded your code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I would also add, since you are new to Python, you really should be using Python 3 not Python 2.  Make sure you check out the link suggested by buran, it will give you a good idea on how a game is structured. Note, however, it is written in Python 3 but it should still be simple enough to follow along.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
(Sep-25-2017, 08:46 PM)ichabod801 Wrote: Refactoring is basically reorganizing your code. The problem is the if/elif/else structure of your code. If you structured your code differently, you wouldn't run into the problems you are having. You could fix your code without restructuring it, by defining dummy values for DSC and the other similar variables at the beginning. But in the long run that structure would lead to other problems as you expanded your code.

How would I restructure it?
Reply
#9
Look at the link that Buran posted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 310 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,121 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,912 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,351 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,521 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,934 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,374 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 15,028 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,404 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,901 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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