+ 1
Whats wrong with this code?
It seems pretty straightforward but why does it print only You Have Nothing no matter what? https://code.sololearn.com/cEiu0uoy6crq/?ref=app
8 Respostas
0
Sword = 1
Shield = 2
Sandals = 3
inventory = [ 1, 2]
if Sword in inventory:
print("You have Sword")
if Shield in inventory:
print("You have Shield")
if Sandals in inventory:
print("You have Sandals")
if Sword and Shield and Sandals in inventory:
print("You have Sword, Shield and Sandals")
if Sword and Shield in inventory:
print("You have Sword and Shield")
if Sword and Sandals in inventory:
print("You have Sword and Sandals")
if Shield and Sandals in inventory:
print("You have Shield and Sandals")
else:
print("You have nothing")
This should work
+ 3
Zinnur Hossain Good luck 👍
+ 2
+ 2
Yeah both of them work, thanks!!!
+ 2
Niek Aukes Try your code with inventory [2, 3].
+ 2
Diego Acero Thank you for mentioning this, I'll change the example.
+ 1
Nick I wouldn’t recommend naming a variable like a built-in function (e.g. str) as it may cause some problems.
0
1(assigned to Sword) doesn't occur in inventory, if you want this to work, create a dictionary or replace "Sword" etc. with 1, 2 or 3 (in inventory). Or replace values with strings