Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:36:39+00:00 2026-06-03T08:36:39+00:00

Recently I’ve been creating a game but I’m having a problem. To calculate what

  • 0

Recently I’ve been creating a game but I’m having a problem. To calculate what you’ve hit on an enemy it uses the variable sword.

At this point the player may or may not have purchased a sword, therefore I get an error that says: “variable not declared”. Is there a way to work around a variable that hasn’t been declared yet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:36:41+00:00Added an answer on June 3, 2026 at 8:36 am

    You have two options.

    The first is to ensure that a player always has a sword variable. Set it to None before the player obtains a sword and, when you need to know whether the player has a sword, check whether the variable is None:

     sword = None
    
     # do lots of stuff here
    
     if sword is None:
        print 'Hey, no sword!'
     else:
        print 'Chop, chop, chop!'
    

    Another way to write this if sword is not allowed to be an empty string or an integer value of 0 is:

     if sword:
        print 'Chop, chop, chop.'
    

    Your second option is to wrap every reference to the sword variable in a try block and swallow the NameError that might be raised:

     try:
        if sword == 10:
            print 'Choppity chop chop chop.'
     except NameError:
        pass
    

    The first solution is (much) better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I asked this question: Find Common Values in several arrays, or lists VB.NET
Recently I had a problem. How to disable scrolling in a particular area of
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently, I've been dealing with an error with accessing MAPI via the .NET framework
Recently our site has been deluged with the resurgence of the Asprox botnet SQL
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently I discovered a problem on the midas and I fixed it, the problem
Recently, we discovered odd behavior in some old code. This code has worked for
Recently,I am handling a solution for WebPart internationalization,but I am not familiar with the
Recently I've been doing quite the project mostly working with the DateTime class. Now,..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.