Beginner - Unexpected Behavior in a Variable

Asked

Viewed 59 times

0

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

I created a variable for the total value I receive and one for the current value, which is the total value minus an input that asks how much I spent today. When running the code, it seems that 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

2 answers

0

In your code, you are calculating MEDIARESTANTE using the fixed value of VRACTUAL 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 you inserted the spent 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 remains should not be shown when you have exceeded the limit of 25 real.

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

  • thank you very much, it helped a lot!

  • You are welcome, Eduardo. If my answer has solved the problem, please accept it by clicking on check in below her grade.

-1

By copying your code on ideone, I’ve actually verified that code behavior is as 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 divisor is decreasing, it increases the remaining average.

Altering in the while the line x = x + 1 for x = x - 1, or 30 - x for 30 + x the average values fall as desired

Code in the 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 calculation expression of the remaining average. Raising the result to -1 and divine by 1000.

  • Hi, thanks for the answer. The problem is that this part seems correct. The splitter is decreasing because there are fewer days left in the month to split the VR, so theoretically this is right. :(

  • @Eduardomergener made some changes to the code

Browser other questions tagged

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