Code creation in Pyhton

Asked

Viewed 322 times

-2

Hello I would like a help, I need to resolve the following question: Suppose a cashier has only 1, 10 and 100 real notes. Considering someone is paying for a purchase, write an algorithm that shows the minimum number of bills the cashier should provide as change. Also show: the purchase value, the change value and the amount of each type of change note. Suppose the monetary system does not use currencies.

I did not understand very well, someone could help me in the realization of the code. To be able to do so far:

value=int(input('----Enter the value the client gave:----'))

change= value - purchase price

if (change <10): >

--- I’m not very good at programming. I thank anyone who can help me :)

  • 1

    If you can’t do it in Python, use "portugol", express the logic using English. Your question embeds two: elaborate the algorithm, and encode this algorithm in Python. One problem at a time :)

1 answer

0


Olá Juliana!

If I understand correctly, your question should receive 2 input values:

  • Value of the price of the product purchased;
  • Amount paid by the customer.

With "inputs" (input data), we have 3 cases to consider:

  1. The amount paid by the customer is less than the price of the product;
  2. The amount paid by the customer is equal to the price of the product;
  3. The amount paid by the customer is greater than the price of the product.

Now, just make a program that gets two values - by the function input(), as you have done - and based on them, use conditional if() for the 3 cases cited above.

So we can start the code as follows:

preco = int(input(' Digite o valor do preço do produto: ')) # Armazena na variável 'preco' o preço do produto, fornecido pelo usuário.
valor_pago = int(input(' Digite o valor pago pelo cliente: ')) # 'valor_pago' armazena o valor pago pelo cliente.

# Com isso, temos 3 casos a considerar, e associaremos os casos pelos condicionais if, elif e else:
if(valor_pago < preco): # Caso em que o valor pago pelo cliente for menor que o preço.
    print('\n O valor pago pelo cliente não é suficiente para a compra do produto.')
elif(valor_pago == preco): # Caso em que o valor pago é igual ao preço do produto. Utiliza-se 'elif' em casos de mais de duas condição.
    print('\n Troco ao cliente é de R$ 0,00.') # Utilizei '\n' para pular linhas no terminal.
else: # Caso o valor pago pelo cliente seja maior que o preço do produto (else), nenhum dos dois casos anteriores.

Then within the parole else, The logic should be applied to calculate the number of banknotes of R$ 100, R$ 10 and R$ 1 that must be returned to the customer. For this, I will use the operator //. This operator is used between two numbers, and returns the entire division between them. As the example below:

10 // 3 = 3

10 / 3 = 3,333333...

Whether it is because 10 / 3 is equal to 3,3333..., or equal to 3 with remainder 1. The operator // returns only the entire split result, discarding the rest.

Therefore, on parole else, I will make use of this operator, for example:

preco = 100

valor_pago = 250

troco = 150

If you want the amount of 100 banknotes (it will only be 1), just do notas_100 = troco // 100, as described below:

troco = valor_pago - preco # Calcula-se o troco.

notas_100 = troco // 100 # Calcula-se a quantidade de notas de R$100,00 a serem pagas ao cliente, pelo operado '//', que calcula a divisão inteira entre dois números.
print(f'\nNotas de R$100,00 para o cliente: {notas_100}') # Mostra ao usuário a quantidade de notas de 100, pela função print(f'{}').

troco = troco - (notas_100 * 100) # Diminui-se do troco a quantidade de notas de 100 multiplicadas 100.
notas_10 = troco // 10 # Mesmo método para notas de R$10,00.
print(f'Notas de R$10,00 para o cliente: {notas_10}')

troco = troco - (notas_10 * 10)
print(f'Notas de R$1,00 para o cliente: {troco}') # O troco restante está entre 0 e 10, portanto a quantidade de notas de R$1,00 é igual ao troco restante.

I think the explanation has become a little complex, but the important part of the code is based only on the conditional else, but it is essential to rely on cases 1 and 2, since it is not always that users do a correct typing.

Any questions, I am available :D

Att, Bruno Kenzo.

  • 1

    Thank you very much, you can clarify very well my doubt, the beginning is easy the part that makes it difficult is in logic, but your explanation was very good!!

  • You’re welcome, Juliana! !

Browser other questions tagged

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