Python Forum
If statment Saying text is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statment Saying text is not defined
#1
Hi I am working on a text based game but I've ran into a slight problem. In the bit where you choose the weapon i use and input but for some reason it takes that input and under my if statment it says your weapon is not defined. If you choose sword as your weapon is says sword is not defined when its not even a variable

from pygame import *
import sys,time,os

Welcome = 'A Villian: Mwa Ha Ha Ha I have come to rain terror upon your village\n\
'
Welcome2 = 'A Villian: And Who are you\n\
'
Chat = 'Well Well Well '
Chat2 = 'You happen to have caught me on a bad day. Im going to have to kill you\n\
what puny weapon do you have this time a sword? a dagger? a mace?\n\
'
Um = 'I\'\ve never heard of that but I guess thats ok'

mixer.init()
mixer.music.load('30 Minutes.ogg')
mixer.music.play()

#while mixer.music.get_busy():
    #time.Clock().tick(10)

def typewriter(Welcome):
    for char in Welcome:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)

def typewriter(Welcome2):
    for char in Welcome2:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)

def typewriter(Chat):
    for char in Chat:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)

def typewriter(Chat2):
    for char in Chat2:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)

def typewriter(Um):
    for char in Um:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)
        Um=True


    


print('An explosion surrounds you as the wind swirls in your face')
time.sleep(1)
typewriter(Welcome)
print('You: Not today old fool')
time.sleep(1)
typewriter(Welcome2)
Name = input ('I am ')
typewriter(Chat)
print (Name + '.')
typewriter(Chat2)
Weapon = input ('I have a ')

Weapon = (Weapon.lower())
print (Weapon)





if not Weapon == sword:
    if Um == False:
        typewrite(Um)
    
if not Weapon == mace:
    if Um == False:
        typewrite(Um)
if not Weapon == dagger:
    if Um == False:
        typewrite(Um)
Reply
#2
Please post the entire error verbatim, as it contains useful information about where the problem occurred.
Reply
#3
if not Weapon == sword: sword is not defined did you mean to check for the string sword if not Weapon == 'sword': (same applies for mace and dagger)
Note: your typewriter functions overwrite each other, you call typewrite in the if statements which is also not defined.
Reply
#4
typewriter(chat) executes the same function as typewriter(chat2). All of your calls to typewriter() call the last definition of typewriter (def typewriter(Um):).

And what is def typewriter(Um) supposed to do:
def typewriter(Um):
    for char in Um:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.05)
        Um=True   # Huh??
I know it types out letters one at a time like a typewriter, but what about the last line in the function? I was surprised that this didn't crash with a message saying "TypeError: 'bool' object is not iterable", but I guess that the way iterators work causes it to ignore that you changed Um from a string you want to display into a bool value. So the "Um=True" at the bottom of the function ends up doing nothing. The global variable "Um" is not changed by assigning a value to local variable "Um" inside the function.

And what is this supposed to do?
if not Weapon == sword:
    if Um == False:  # What??
        typewrite(Um)
Um is always going to be 'I\'\ve never heard of that but I guess thats ok'. An If Um == False is always goint to return False, so none of tye typewrite® calls will be executed.

I think your game is quickly entering a death spiral. There are too many things going on that you don't really understand and that causes you to make more mistakes that causes you to make more of a mess.

I would restart with a much simpler game that only does the music and the welcome. Make sure the typewriter function works and that you only have one typewriter function.

Once you get that working perfectly, write a second program that only does the choose weapon part. When you get that code to work you can integrate it into the welcome code. Slowly add little bits of functionality piece by piece, testing each piece to make sure it works correctly, then integrating into the main code. If you are familiar with source control tools you can make faster progress letting the source control tool do most of the branching and integration work, but the process is still much the same. Get something that works and keep it. Make small modifications and test. Always have backups in case you mess everything up.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,879 May-30-2020, 04:14 AM
Last Post: DT2000
  Pylint Statment Count Shangeetha 1 2,044 Dec-22-2019, 04:58 PM
Last Post: Larz60+
  Error in print statment leodavinci1990 1 1,941 Jul-21-2019, 04:46 AM
Last Post: buran
  saving (in text or binary) an object under a defined class cai0824 3 3,093 May-12-2019, 08:55 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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