Beginner - Unexpected behavior in a variable

0

I'm trying to create a code in Python that calculates the remaining value in my VR and alerts me if the average per day drops to 25. It's an attempt to understand better while and break.

I created a variable for the total amount I get and one for the current value, which is the total amount minus one input that asks how much I spent today. When I run the code, it looks like the value is being added to my total value, rather than subtracted. Does anyone know where I went wrong?

PS: X = is the number of days counted in a month.

GASTODIA = int(input("Quanto você gastou hoje almoçando?"))
VRTOTAL = (35 * 30)
VRATUAL = (35 * 30) - (GASTODIA)
x = 1
MEDIARESTANTE = VRATUAL/(30 - x)

while MEDIARESTANTE > 25:
    GASTODIA = int(input("Quanto você gastou hoje almoçando?"))
    x = x + 1
    MEDIARESTANTE = VRATUAL / (30 - x)
    print("Você ainda tem " + str(MEDIARESTANTE) + " por dia para gastar.")
    if MEDIARESTANTE < 25:
        print("Pisa no freio, amigão, você só tem 25 reais por dia agora")
        break
    
asked by anonymous 10.12.2018 / 18:32

2 answers

0

In your code, you are calculating MEDIARESTANTE using the fixed value of VRATUAL and the variable value of x. That is, you are dividing the same starting value for fewer and fewer days.

VRTOTAL = (35 * 30)
VRATUAL = (35 * 30)
x=1
MEDIARESTANTE = VRATUAL/(30 - x)

while MEDIARESTANTE > 25:
    GASTODIA = int(input("Quanto você gastou hoje almoçando?\n")) #Acrescentei um \n para imprimir o valor gasto em nova linha
    VRATUAL = VRATUAL - GASTODIA #Atualizando valor remanescente
    MEDIARESTANTE = VRATUAL / (30 - x)
    x = x + 1   #Prepara para o próximo dia
    if MEDIARESTANTE <= 25:
        print("Pisa no freio, amigão, você só tem {} reais por dia agora".format(MEDIARESTANTE))
        break
    else: #Dessa forma o programa não dirá que você ainda tem tanto antes de mandar você pisar no freio
        print("Você ainda tem {} por dia para gastar.".format(MEDIARESTANTE))

I removed the part that you would put the spending of the first day out of the loop. It is not good practice to do the same action inside and outside the loop. The message about how much left over should not be shown when you have exceeded the $ 25 limit.

When entering the loop you enter the amount spent on the first day (x = 1) and calculate the remaining average value (VRATUAL / (30-x)) for the next 29 days. Then the value of x is incremented so that in the next iteration of the loop it is the second day and the average is made over the remaining 28 days.

    
13.12.2018 / 06:00
-1

Copying your code into ideone, I actually checked that the behavior of the code is what you say.

The problem is that each time you are increasing x ( x = x + 1 ) and then you subtract that value from 30 30 - x . As the current value divider is decreasing, it increases the remaining mean.

By changing while the line x = x + 1 to x = x - 1 , or 30 - x to 30 + x mean values fall as desired

Code in ideone .

GASTODIA = int(input("Quanto você gastou hoje almoçando?"))
VRTOTAL = (35 * 30)
VRATUAL = (35 * 30) - (GASTODIA)
x = 1
MEDIARESTANTE = VRATUAL/(30 - x)

while MEDIARESTANTE > 25:
    GASTODIA = int(input("Quanto você gastou hoje almoçando?"))
    x = x + 1
    MEDIARESTANTE = VRATUAL / (30 - x) ** -1 / 1000
    print("Você ainda tem " + str(MEDIARESTANTE) + " por dia para gastar.")
    if MEDIARESTANTE < 25:
        print("Pisa no freio, amigão, você só tem 25 reais por dia agora")
        break

EDIT

I changed the expression expression for the remaining mean. Raising the result to -1 and dividing by 1000.

    
10.12.2018 / 18:45