2017-10-19 6 views
0

J'essaie donc d'arranger le fonctionnement du gameplay de mon jeu de base de texte si le joueur peut/ne dispose pas de certains éléments dans son inventaire.Aide à l'inventaire Python

print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit") 
    if "comb" and "razor" in inventory: 
     print ("and the table with the empty bottle.") 
    else "comb" not in inventory and "razor" in inventory: 
     print ("and the table with the empty bottle and comb.") 
    else "razor" not in inventory and "comb" in inventory: 
     print ("and the table with the empty bottle and razor") 

il me dit que j'ai une erreur de syntaxe dans cette ligne de code

else "comb" not in inventory and "razor" in inventory: 

Je ne peux pas l'impression de voir quelle erreur j'ai fait, je suis un débutant alors peut-être il y a une façon alternative d'exécuter mes besoins.

+8

Je pense que vous voulez dire 'elif', pas' else'. – khelwood

Répondre

0

Vous y êtes presque

Les else ne fonctionne que de cette façon

else: 
    do something 

donc votre code serait comme ça

print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit") 
    if "comb" and "razor" in inventory: 
     print ("and the table with the empty bottle.") 
    elif "comb" not in inventory and "razor" in inventory: 
     print ("and the table with the empty bottle and comb.") 
    elif "razor" not in inventory and "comb" in inventory: 
     print ("and the table with the empty bottle and razor") 

Ou que

print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit") 
    if "comb" and "razor" in inventory: 
     print ("and the table with the empty bottle.") 
    elif "comb" not in inventory and "razor" in inventory: 
     print ("and the table with the empty bottle and comb.") 
    else: #using the else here 
     print ("and the table with the empty bottle and razor") 

Cependant, lorsque j'ai testé votre code, j'ai réalisé que la façon dont vous avez mis votre logique ne fonctionnera pas correctement.

En utilisant cette if all(x in inventory for x in ['comb','razor']) traitera correctement la présence des deux variables, comb et razor dans inventory et permettre aux autres conditions à laminer de manière correcte si l'on de l'autre valeur est manquante.

inventory = ['comb','razor'] 
#inventory = ['razor',''] 
#inventory = ['comb'] 

print("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up ju 
mpsuit") 
if all(x in inventory for x in ['comb','razor']): 
    print ("and the table with the empty bottle.") 
elif ('comb' not in inventory) and ('razor' in inventory): 
    print("and the table with the empty bottle and comb.") 
elif ('razor' not in inventory) and ('comb' in inventory): 
    print("and the table with the empty bottle and razor") 
0
print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit") 
    if "comb" and "razor" in inventory: 
     print ("and the table with the empty bottle.") 
    elif "comb" not in inventory and "razor" in inventory: 
     print ("and the table with the empty bottle and comb.") 
    elif "razor" not in inventory and "comb" in inventory: 
     print ("and the table with the empty bottle and razor")