K
K
ki vi2019-06-19 17:19:17
Python
ki vi, 2019-06-19 17:19:17

How to make a function work in python?

function does not work completely.

def  mest(x,y):
    global money, eat, ruda , woter, slip, skura, slitcIron , vill1Na ,vill2House,vill1House,villRoz, villNA
    global edaloshad,eda,kirka,mechh,udocha,bronAll,topor
    print("здоровье: "+str(zdorow)+"\nденьги: "+str(money)+"\nеда: "+str(eat)+"\nвода: "+str(woter)+"\nсон"+str(slip))

    if ((x==1 and y==1) or(x==3 and y==1)):
        if x==2 and y==1:
            vill1Na= vill1Na
            villRoz=vill1Roz
            villDov=vill1Dov
            villDolg=vill1Dolg
            villHouse= vill1House
        else:
            villNA = vill2Na
            villRoz = vill2Roz
            villDov = vill2Dov
            villDolg = vill2Dolg
            villHouse = vill2House
        print("Ты находишься в деревне"+ villNA)
        if villRoz ==10:
            money= money - 3000
        elif villRoz== 9:
            money= money- 2700
        elif villRoz== 8:
            money= money- 2400
        elif villRoz== 7:
            money= money- 2100
        elif villRoz == 6:
            money = money - 1800
        elif villRoz == 5:
            money = money - 1500
        elif villRoz == 4:
            money = money - 1200
        elif villRoz == 3:
            money = money - 900
        elif villRoz == 2:
            money = money - 600
        elif villRoz == 1:
            money = money - 300
        deystv= int(input("Места:\n1)таверна\n2)рынок\n3)кузница\n4)дома\n6)граница\n7)рюкзак\n: "))

        if deystv== 1:
            dey2= input("действия:\n1)купить еды\n2)попить\n3)снять комнату\n4)назад")
            if dey2 == 1:
                print("еда: "+eat)
                dey3= input("1)маленькая порция\n2)средняя порция\n3)большая порция")
                if dey3== 1:
                    eat= eat + 20
                    money=money-10
                elif dey3==2:
                    eat= eat+ 27.5
                    money = money - 15
                elif dey3==3:
                    eat = eat+35
                    money = money - 20
                if eat > 100:
                    eat = 100
                print("Ты поел")
                mest(geox,geoy)

            elif dey2==2:
                woter= woter+40
                if woter>100:
                    woter=100
                print("Ты попил")
                mest(geox,geoy)
            elif dey2==3:
                print("сон: "+slip)
                dey3= input("1)Короткий сон\n2)средний сон\n3)большой сон\n4)пока не выспишся\n(0.5 за еденицу)\n")
                if dey3==1:
                    slip= slip+20
                    money= money-10
                elif dey3==2:
                    slip=slip+35
                    money=money-17.5
                elif dey3==3:
                    slip= slip+50
                    money=money-25
                elif dey3==4:
                    ostatsp= 100-slip
                    plata= ostatsp*0.5
                    money=money-plata
                if slip>100:
                    slip=100
                mest(geox,geoy)
            elif dey2==4:
                mest(geox, geoy)
            else:
                mest(geox, geoy)

the second if(dey2) in the function does not work, help!!!
If you need the complete file, I can send it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Yakushenko, 2019-06-19
@kirillvoroshilov

edaloshad is \nfood
The input() operation returns a string, and you have a check for an int. Do this:
if dey2 == "1":

T
tsarevfs, 2019-06-19
@tsarevfs

I offer you an option on how to make the code more beautiful.

places = ["Таверна","Рынок"]
tavern_actions = ["Купить еды","Пить"]
marketplace_actions = ["Купить руду","Продать руду"]

def chose_option(options):
  option_strings = ["{}) {}".format(i + 1, name) for (i, name) in enumerate(options)]
  question = '\n'.join(option_strings) + '\n'

  return int(input(question))

class Player:
  def __init__(self):
    #конструктор вызывается при создании
    self.money = 0
    self.water = 0
    self.food = 0

  def __repr__(self):
    return "Player:\n money: {}\n water: {}\n food: {}\n".format(
      self.money, self.water, self.food
      )

  def tavern(self):
    action = chose_option(tavern_actions)
    #if action == 1:
    # 	self.buy_food()
    #...

  def marketplace(self):
    action = chose_option(marketplace_actions)
    #...

  def mest(self, x, y):
    print(self)

    if ((x==1 and y==1) or(x==3 and y==1)):
      pass
      #...

    place = chose_option(places)
    if place == 1:
      self.tavern()
    if place == 2:
      self.marketplace()

p = Player()
p.mest(0, 0)

We use a class instead of global variables. We break down different actions into functions.
What else can be done:
The World class, which stores the villages array, and possibly something else
The Village class, which stores the position, name, ...
Then the terrible if:
if ((x==1 and y==1) or(x==3 and y==1)):
    if x==2 and y==1:

Can be replaced with
village = world.find_village(x, y)
if (village != none):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question