-2

Everytime this piece of code is compiled, I expect it to go through each "if,elif" untill it finds a match. So it can continue to print out the given list on the screen.

But to my surprise even if you input "rifles", all it does is give you the "else" statement.

This is my first post on Stackoverflow, so forgive me if I made some mistake.

Any help is very much appreciated, thanks in advance.

rifles = ["Ak47", "M4A1", "Aug", "Famas", "Galil"]
pistols = ["Glock", "USP", "Deagle", "P250", "Five-7", "Tec-9"]
shotguns = ["Buckshot", "Benelli", "M1319", "Sawed-Off"]
snipers = ["AWP", "SSG", "Cheytac", "Barret", "M24"]
grenades = ["Smoke", "High-Explosive", "Flash", "Concussion", "Molotov", "Incendiary"]
knives = ["Bayonette", "Karambit", "Machete", "Butterfly", "Katana"]
equipment = ["Kevlar", "Helmet", "Tactical-Shield", "Boots", "Nightvision"]

raw_input = input("Search: ")


def search():

    if raw_input == "Rifles":
        for ri in rifles:

            print(ri)
            break
    elif raw_input is "Pistols":
        for pi in pistols:

            print(pi)
            break
    elif raw_input is "Shotguns":
        for sho in shotguns:

            print(sho)
            break
    elif raw_input is "Snipers":
        for sni in snipers:

            print(sni)
            break
    elif raw_input is "Grenades":
        for gre in grenades:

            print(gre)
            break
    elif raw_input is "Knives":
        for kni in knives:

            print(kni)

    elif raw_input is "Equipment":
        for equ in equipment:

            print(equ)
    else:

        print("Try again...")
    return
search()
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119

3 Answers3

0

You are breaking each loop after you only print one element.

if raw_input == "Rifles":
    for ri in rifles:
        print(ri)
        break

You don't really need the break, so get rid of it and it will work when you type "Rifles"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Your code can be expressed in a much more concise and "pythonic" way:

guns = {
    "rifles"   : ["Ak47", "M4A1", "Aug", "Famas", "Galil"],
    "pistols"  : ["Glock", "USP", "Deagle", "P250", "Five-7", "Tec-9"],
    "shotguns" : ["Buckshot", "Benelli", "M1319", "Sawed-Off"],
    "snipers"  : ["AWP", "SSG", "Cheytac", "Barret", "M24"],
    "grenades" : ["Smoke", "High-Explosive", "Flash", "Concussion", "Molotov", "Incendiary"],
    "knives"   : ["Bayonette", "Karambit", "Machete", "Butterfly", "Katana"],
    "equipment": ["Kevlar", "Helmet", "Tactical-Shield", "Boots", "Nightvision"],
}

gun = raw_input("Search: ")
gun = gun.lower()

try:
    print guns[gun]
except KeyError:
    print "No gun of type %s" % gun
PabloG
  • 25,761
  • 10
  • 46
  • 59
  • This looks very interesting, should probably add that I use 3.5 Python. – Georges St. Pierre May 01 '16 at 21:05
  • All you have to change to use the code in Python 3.5 is `input` for `raw_input` and `print (x)` instead of `print x` – PabloG May 01 '16 at 21:08
  • Wonderful, I got it to compile and it does indeed seem much more simplistic than the previous method that was used. However I would love to have the same output layout as before where the for loop got all the strings to go on top of eachother. Any idea how one would get that done? – Georges St. Pierre May 01 '16 at 21:13
  • change `print guns[gun]` to `for x in guns[gun]: print (x)` or even `print ("\n".join(guns[gun]))` – PabloG May 01 '16 at 21:21
  • Absolutely amazing Pablo, this was a real eye opener! – Georges St. Pierre May 01 '16 at 21:28
-1

But to my surprise even if you input "rifles", all it does is give you the "else" statement.

Your code checks for "Rifles", not "rifles". If you want the two to compare equal, you need to normalise the letter case, for example, like so:

if raw_input.lower() == "rifles":
    for ri in rifles:
        ...
NPE
  • 486,780
  • 108
  • 951
  • 1,012