Reputation: 121
def equip(item):
x = True
while x == True:
if len(bag_sword) > 1:
print "You can't have 2 weapons equipped!"
break
if len(bag_chest) > 1:
print "You can't have 2 chestplates equipped!"
break
if len(bag_gloves) > 1:
print "You can't have 4 gloves equipped!"
break
if len(bag_helmet) > 1:
print "You can't have 2 helmets equipped!"
break
if item == "iron sword" and "iron sword" in bag:
print "\nYou equip the Iron Sword"
bag.remove("iron sword")
bag_sword.append("iron sword")
break
Whenever I run this function a second time after I put "iron sword" into one of the bag_
s, and try to add another to test it, it doesn't do anything and the code completely freezes.
The bag_
s are all lists.
My guess was the while loop was stuck and it wasn't changing the length whenever I put something into the list in-game, because that's not how the language works?
Upvotes: 0
Views: 1022
Reputation: 547
The entire code is nonsense. First of all, you are dealing with lists and not dictionaries.
You DON'T have in Python "break" keyword. In Python exist if, elif and else keywords.
item is the parameter you pass in the function.
the keyword len(['item','two']) gives as a resultset 2, lists have only values and the elements are fetched by number in the list.
example: mylist = ['hello','world']
print mylist[0] results in 'hello'
get yourself more familiar with builtin types: https://docs.python.org/2/library/stdtypes.html
Here is some sample code:
bag_s = {'bag_sword':0,'bag_chest':0,'bag_gloves':0,'bag_helmet':0,'iron sword':0}
def equip(item):
its = item.keys()
for itx in its:
if itx == 'bag_sword':
if item[itx] > 1:
print 'you can\'t have 2 weapons erquipped'
elif itx == 'bag_chest':
if item[itx] > 1:
print 'you can\'t have 2 weapons erquipped'
elif itx == 'bag_gloves':
if item[itx] > 1:
print 'you can\'t have 2 weapons erquipped'
elif itx == 'bag_helmet':
if item[itx] > 1:
print 'you can\'t have 2 weapons erquipped'
elif itx == 'iron sword':
if item[itx] > 1:
print 'you can\'t have 2 weapons erquipped'
if item[itx] == 1:
bag_s[itx]+=1
equip({'bag_sword':1})
print bag_s
Upvotes: 0
Reputation: 1121266
Once you added a sword to the bag, none of your conditions are true. Your len(bag_sword)
is exactly 1, (so if len(bag_sword) > 1:
won't be matched), and 'iron sword' in bag
is false.
Because none of your if
statements match, no break
is executed and your while
loop goes on forever.
You don't want to test if there is more than one item in your bag_sword
. Test if there is any item in that bag:
if bag_sword: # true if not empty
print "You can't have 2 weapons equipped!"
break
because you are testing before adding a second weapon.
Upvotes: 3