One of my else statements in python doesn´t work.

时间:2018-03-22 23:39:03

标签: python

I´m trying to make a textbased game. What the else statement is supposed to do is if the player doesn´t type in an item they have it tells them so, then restarts the method. What really happens is when I run the code is that I get an invalid syntax for my troubles. The else statement has a * next to it.

def fightMode():
      action=raw_input("Type a to attack, s to pick an item, d to runaway.Then hit enter")
      if action=="s":
        pickItem()
      elif action == "a":

def isPrime():
  defense=10
  strength=10
  Health= 10
  items={"shepards crow":strength+2 and defense+2}
  equipedItems=[]
  print("You're quietly herding the village's sheep when you suddenly hear a huge roar. Goblns have invaded the village and are coming towards you. Do you A.Runaway B.Fight C.Herd the sheep away")
  answer=rawinput("Type your option A,B, or C and press enter")

def pickItem():
  itemPick=raw_input("Type in your item and press enter")
  for x in items:
    if itemPick == x:
      equipedItems=[x]
      print("% equiped" %itemPick)
    fightMode()
    *else:
      print("Item unavailable. Try again")
      pickItem()*

def Print():
  print("Health= %" %Health)
#this is enacted when you get into a fight

1 个答案:

答案 0 :(得分:0)

fightmode() needs to be intended to the same level as the rest of the if block:

def pickItem():
  itemPick=raw_input("Type in your item and press enter")
  for x in items:
    if itemPick == x:
      equipedItems=[x]
      print("% equiped" %itemPick)
      fightMode()
    else:
      print("Item unavailable. Try again")
      pickItem()

The way you had it the if statement was ended by the fightmode() statement causing the else to be a syntax error because there was no open if statement for the else to relate to.

Also, use 4 spaces to indent per level. It is the standard and what everyone reading your program will expect.