Skip to content

Instantly share code, notes, and snippets.

@MrDMurray
Last active September 29, 2016 08:42
Show Gist options
  • Save MrDMurray/a46296a0118e4a6e4c12e7dde26866f4 to your computer and use it in GitHub Desktop.
Save MrDMurray/a46296a0118e4a6e4c12e7dde26866f4 to your computer and use it in GitHub Desktop.
STJLOL-DMU-for loops - Search
#This program accepts an input from the user and then searches through a list to see if there is a match.
#It then counts how many matches there are.
bag = ["raspberry", "pokeball", "pokeball", "pokeball", "great ball", "potion"] #This is the list of items in the bag
print (bag) #Prints the contents of the bag
count = 0 #starts the count at 0
while True:
search = input("Search for what?")
for item in bag: #starts going through list, checking if each item = pokeball
if item == search: #checks if the item matches a pokeball
print("Hey I found a "+ search) #prints only if the item = the user's search input
count = count + 1 #As the item matched, this increases the count by one
print (count) #prints the count so you can see it count
else:
print("What's this? Nope, not a "+search) # If item is not equal to search, prints not a whatever you searched
print("looks more like a " + item) #prints the name of the item the for loop is currently kooking at
print("I found %d in total." % count) #when the for loop is finished, this prints the final count
count = 0 #resets the count so you can search again
"""
OUTPUT LOOKS LIKE:
Search for what?pokeball
What's this? Nope, not a pokeball
looks more like a raspberry
Hey I found a pokeball
1
Hey I found a pokeball
2
Hey I found a pokeball
3
What's this? Nope, not a pokeball
looks more like a great ball
What's this? Nope, not a pokeball
looks more like a potion
I found 3 in total.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment