Why transfer the variable of entry into another?

Asked

Viewed 35 times

-4

In one of the examples, on page 92 of the book "Introduction to Programming with Python: Algorithms and Programming Logic for Beginners - Nilo Ney Coutinho" he gives us the code below, but I am doubtful why he replaces the variable 'delete' with 'value', could not simply use the variable "value". I’m starting now in programming on my own and would like to understand why.

valor = int(input("Digite o valor a pagar:"))
cédulas = 0
atual = 50
apagar = valor

while True:
   if atual <= apagar:
       apagar -= atual
       cédulas +=1
   else:
      print(f"{cédulas} cédulas de R$ {atual}")
      if apagar == 0:
          break
      if atual == 50:
          atual = 20
      elif atual == 20:
          atual = 10
      elif atual == 10:
          atual = 5
      elif atual == 5:
          atual = 1
      cédulas = 0
  • 1

    There is no need to use the variable apagar. I understand that the creator of this code used two variables because it is changing the value of apagar then, which means that if he needed to use again the original value entered by the user, he would still have this value saved in the variable valor. This is not the case, but it can still be considered a good practice.

1 answer

-2

Let’s assume at the end of the program you wanted to show the following message:

print(f"O usuário fez o saque de R$ {valor} com sucesso")

Also suppose the user provides 15 as input to the program.

If you manipulated the variable valor, the program would print "The user made the withdrawal of R $ 0 successfully", since it is being decreased to 0 in lines 8 and 12. When you create a new variable apagar and manipulates this variable instead, the program stores the original value of valor and prints "The user made the withdrawal of R $ 15 successfully".

Browser other questions tagged

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