I cannot return assigned values to variables

Asked

Viewed 143 times

1

I am layman programming myself and decided to start a program in python to use as a tool in a game well, the code so far is this:

def tempo (temp):
total=temp*quant
dias=total//(24*60*60)
var1=total-(dias*(24*60*60))
horas=var1//(60*60)
var2=var1-(horas*(60*60))
minu=var2//60
seg=var2-(minu*60)
print('O tempo é de %d dias, %d horas, %d minutos e %d segundos'%(dias,horas,minu,seg))

def velocidade(nome,nivel):
time = nome * (2/3)/1.06**nivel
tempo(time)

def custo (mad,arg,fer,faz):
mad_final= mad * quant
arg_final= arg * quant
fer_final=fer * quant
faz_final=faz * quant
print('O custo será de %d de maidera, %d argila e %d de ferro!\n %d de população na fazenda'%(mad_final,arg_final,fer_final,faz_final))


# Custos tempo
Lanceiro=1020
Espadachim=1500
Barbaro=1320
Arqueiro=1800
Explorador=900
Cavalaria_leve=1800
Arqueiro_cavalo=2700
Cavalaria_pesada=3600
Ariete=4800
Catapulta=7200

# Custo recursos

Lan=(50,30,10,1)
Esp=(30,30,70,1)
Bar=(60,30,40,1)
Arq=(100,30,60,1)
Exp=(50,50,20,2)
Leve=(125,100,250,4)
Arq_cav=(250,100,150,5)
Pesada=(200,150,600,6)
Ari=(300,200,200,5)
Cat=(320,400,100,8)

# Começo

print('###########################\n# O que você deseja calcular? #\n###########################')
var=int(input ('1 - Tempo de recrutamento \n2 - Custo de recursos\n3 - Todas as anteriores\n'))
#Recrutamento
if var == 1 :
    print('Qual tropar?\n')
    trop= int(input ('1 - Lanceiro\n2 - Espadachin\n3 - Barbaro\n4 - Arqueiro\n5 - Explorador\n6 - Cavalaria leve\n7 - Arqueiro a cavalo\n8 - Cavalaria pesada \n9 - Ariete\n10 - Catapulta\n '))
    quant=int(input ('Qual a quantidade?\n'))
#Quartel
    if (1<=trop<=4):
        nivel=int(input('Nivel do Quartel:\n'))
        if trop==1:
            velocidade(Lanceiro,nivel)
        elif trop==2:
            velocidade(Espadachim,nivel)
        elif trop==3:
            velocidade(Barbaro,nivel)
        elif trop==4:
            velocidade(Arqueiro,nivel)
#Estabulo
    elif(4<trop<=8):
        nivel=int(input('Nivel do Estabulo:\n'))
        if trop==5:
            velocidade(Explorador,nivel)
        elif trop==6:
            velocidade(Cavalaria_leve,nivel)
        elif trop==7:
            velocidade(Arqueiro_cavalo,nivel)
        elif trop==8:
            velocidade(Cavalaria_pesada,nivel)
#Oficina
    elif(9<=trop<=10):
        nivel=int(input('Nivel do Oficina:\n'))
        if trop==9:
            velocidade(Ariete,nivel)
        elif trop==10:
            velocidade(Catapulta,nivel)
#Custo
elif var==2:
    print('Qual tropar?\n')
    trop= int(input ('1 - Lanceiro\n2 - Espadachin\n3 - Barbaro\n4 - Arqueiro\n5 - Explorador\n6 - Cavalaria leve\n7 - Arqueiro a cavalo\n8 - Cavalaria pesada \n9 - Ariete\n10 - Catapulta\n '))
    quant=int(input ('Qual a quantidade?\n'))
    #Quartel
    if (1<=trop<=4):
    if trop==1:
        custo(Lan)

My problem is in the cost function, because I can’t make the values assigned to the variable "Lan" by examples work within the function, how could solve this problem?

  • Please correct the code indentation and describe what each part should do. Are you already building algorithms before trying to program in some language? If not, it should before anything.

1 answer

2


The problem is in the number of arguments of the cost function. Note the code below:

def teste(a,b):
    print(a)
    print(b)

if __name__ == "__main__": #O código abaixo será executado caso o programa seja executado
    tupla = [1, 2]
    teste(tupla)

This program generates the following exception,

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
TypeError: teste() missing 1 required positional argument: 'b'

which in Portuguese translates, roughly, to teste() tem um argumento posicional faltando: 'b'. This means that in the test function call, the program assigns a = tupla, but had nothing to attribute to b.

There are some ways to correct the function call teste(tupla):

  • Accessing the positions of the tuple in the call: teste(tupla[0], tupla[1])
  • Unpacking the tuple: teste(*tupla)

In your example, replace:

if trop==1:
    custo(Lan)

for

if trop==1:
    custo(*Lan)

or by

if trop==1:
    custo(Lan[0], Lan[1], Lan[2], Lan[3])

that the programme would work.


Obs: the lines

if (1<=trop<=4):
if trop==1:
    custo(Lan)

appear to contain syntax (no extra indentation after the line below if) and logic errors.

  • That was when I passed here to the site, I did the bill today!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.