Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with elif?
#1
Okay, so this is a snippet of my code (the very beginning of it) that I have isolated in a separate file because it is dysfunctional. We are supposed to create a zombie survival game, but in this code here whenever you input anything other than "spas", it loops back to start (aka "Choose weapon: "). I've been fighting with this for a long time and now I've given up. I don't know what to do. I'm new to Python so please forgive the mess, I just need to know why my elif is just completely disregarded. I'm using Python 2.7.13

crowbar = 'crowbar' and 'Crowbar' and 'crow bar' and 'Crow bar' and 'Crow Bar' and 'bar' and 'Bar'
spas = "Spas 12" and "spas 12" and "spas12" and "Spas" and "Spas12" and "spas"
glock = "glock" and "glock 17" and "glock17" and "Glock" and "Glock 17" and "Glock17"


for turn in range(100):
    weapon = raw_input("Choose weapon: ")
    if weapon == spas:
        weapon == "Spas 12"
        print ('You chose the Spas 12.')
        print ("\n-----------------------------------------------------------\n")
        for turn in range(100):
            name = raw_input("\nNow, what is your name? ")
            if (not name.strip()):
                print ("\nPlease enter your name.")
                name = raw_input("\nNow, what is your name? ")
            if (name.strip()):
                yesno = raw_input("\nOh, so your name is %s? Type \'yes\' or \'no\'. " % name.title())
                if yesno in ["yes", "Yes", "y", "Y"]:
                    print ("")
                    print ("\n-----------------------------------------------------------\n")
                    break
                elif yesno in ["No", "no", "n", "N"]:
                    print ("\nHmm, that\'s not your name?")
                else:
                    print ("\nPlease enter your name and confirm it with yes or no.")
            else:
                print ("\nHmm, that\'s not your name?")
        print ("You turn around. To your left is a gun shop, and to your right is a hospital. Keep in mind you just got into a car crash, but you never know what could be out there. Choose wisely, %s." % name.title())   
        break
    elif weapon == glock:
            weapon == "Glock 17"
            print ("You chose the Glock 17")
            print ("\n-----------------------------------------------------------\n")
            for turn in range(100):
                name = raw_input("\nNow, what is your name? ")
                if (not name.strip()):
                    print ("\nPlease enter your name.")
                    name = raw_input("\nNow, what is your name? ")
                if (name.strip()):
                    yesno = raw_input("\nOh, so your name is %s? Type \'yes\' or \'no\'. " % name.title())
                    if yesno in ["yes", "Yes", "y", "Y"]:
                        print ("")
                        print ("\n-----------------------------------------------------------\n")
                        break
                    elif yesno in ["No", "no", "n", "N"]:
                        print ("\nHmm, that\'s not your name?")
                    else:
                        print ("\nPlease enter your name and confirm it with yes or no.")
                else:
                    print ("\nHmm, that\'s not your name?")
            print ("You turn around and to your left is a gun shop and to your right is a hospital. Keep in mind you just got into a car crash, but you never know what could be out there. Choose wisely.")
            break
    elif weapon == crowbar:
            weapon == "crowbar"
            print ("You chose the crowbar.")
            print ("\n-----------------------------------------------------------\n")
            for turn in range(100):
                name = raw_input("\nNow, what is your name? ")
                if (not name.strip()):
                    print ("\nPlease enter your name.")
                    name = raw_input("\nNow, what is your name? ")
                if (name.strip()):
                    yesno = raw_input("\nOh, so your name is %s? Type \'yes\' or \'no\'. " % name.title())
                    if yesno in ["yes", "Yes", "y", "Y"]:
                        print ("")
                        print ("\n-----------------------------------------------------------\n")
                        break
                    elif yesno in ["No", "no", "n", "N"]:
                        print ("\nHmm, that\'s not your name?")
                    else:
                        print ("\nPlease enter your name and confirm it with yes or no.")
                else:
                    print ("\nHmm, that\'s not your name?")
            print ("You turn around and to your left is a gun shop and to your right is a hospital. Keep in mind you just got into a car crash, but you never know what could be out there. Choose wisely.")
            break
    else:
        print ('Error')
Reply
#2
change your spas assignemnts as well as crowbar/glock to a list
spas = ["Spas 12","spas 12","spas12","Spas","Spas12","spas"]
and then change == to in
    if weapon in spas:
You should be using a while loop at first not a for loop.

Quote: weapon == "Spas 12"
== is comparison, while = is assignment, you want only one

Quote: if (not name.strip()):
print ("\nPlease enter your name.")
name = raw_input("\nNow, what is your name? ")
if (name.strip()):

The second if in the same structure needs to be elif. However it can be just else; because it either is or it isnt. You also dont need to wrap those conditions in parenthesis. It just clogs up the code.
Recommended Tutorials:
Reply
#3
The problem (or at least one of them), is your first three lines. And compares two things. If they are both True, it returns the one on the right. Any non-empty string counts as True. So after your first three lines, spas == 'spas', crowbar == 'Bar', and glock = 'Glock17'.

What you probably want to do is make list of the lower case versions, and then use lower on the input and test if it is in the list.

spas = ['spas 12', 'spas12', 'spas']
weapon = raw_input('Choose weapon: ')
print(weapon.lower() in spas)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Sep-05-2017, 01:03 AM)metulburr Wrote:
    if weapon in spas:
You should be using a while loop at first not a for loop.

How would I change the for to a while loop? I played around with it but sometimes it just gave me errors and other times it loops forever when you input data.
Reply
#5
change the outer for loop to a while loop. Its condition could even be just True. Then break out of it on your else clause where the print error is.
Recommended Tutorials:
Reply
#6
It looks like I got it... Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Functions and if elif problem Bruizeh 2 3,770 Aug-27-2021, 03:37 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

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