Help with While loop in Python

Asked

Viewed 754 times

1

I have tried several times, I can print the largest amount, I can print the date of the biggest sale, my problem is at the time of the loop, code follows the loop as this in command, only when he wants, help me , I spent hours trying to find the error.

  1. A record store notes daily during the month of March the amount of records sold. Determine on which day of that month the largest sale took place and what was the amount of records sold on that day.

Follows the code:

def main():

 qtd=int(input("Informe a quantidade"))

valor=2

 i=1

 dia=0

 maior=0

 while i <=5:

 maior=qtd

 i = i + 1

 qtd = int(input("Informe a quantidade"))

 if maior > qtd:

 maior_qtd=maior

 dia=i-1

 maior_venda=maior_qtd*valor

 print("A maior venda foi {} ".format(maior_qtd))

 print("O valor da maior venda foi {}R$ ".format(maior_venda))

 print("A maior venda foi no dia {}/3".format(dia))


main()

Upshot :

Informe a quantidade 9
Informe a quantidade 4
A maior venda foi 9 
O valor da maior venda foi 18R$ 
A maior venda foi no dia 1/3
Informe a quantidade

The loop is 1 through 5 - and it only does two repetitions.

1 answer

1


There’s a lack of indentation in the code, but I’m not gonna judge you on that. Well, I read the question and developed something similar above and with comments for the explanation of some doubts. I tried to be the most faithful to your code, changed some variable names, treated things a little differently. Study it and send feedback, please.

OBS: "Determine on which day of that month the highest sales occurred and how much of the records sold that day." So I did not put the value as in the case you put, but that’s it. It serves as a basis for study. Brother hugs!

def main():
# Definindo as variaveis
  maior_venda         = int
  dia                 = int

# Atribundo Variaveis
  i           = 1 # Variavel que auxilia no ciclo
  maior_venda = 0 # maior venda == 0 pois não existe maior venda antes de vender algo.

  while i <= 5:

    qtd_vendida = int(input("Informe a quantidade de discos vendidos: ")) # Esse input trabalha com o ciclo, nao necessita de algo fora.

    if(qtd_vendida > maior_venda): # Verificamos se quantidade vendida e maior. Caso sim, atribua o valor a maior venda
        maior_venda = qtd_vendida #atribuimos a maior_venda a maior quantidade vendida
        dia = i # aqui ele verifica a posicao de i, caso quantidade vendida > que a maior venda anterior atribui a dia.

    i += 1; # adiciona +1 a variavel (i), controlando o ciclo.

  print("A maior quantidade de discos vendidos foi: {} discos".format(maior_venda))
  print("A maior quantidade de vendas foi feita dia: {}/MAR".format(dia))

main()

Test 1: Enter the quantity of sold discs: 25
Enter the amount of disks sold: 10
Enter the amount of disks sold: 50
Enter the amount of disks sold: 39
Enter the amount of disks sold: 40

The largest number of albums sold was: 50
The largest amount of sales was made day: 3/MAR

Test 2: Enter the amount of disks sold: 12
Enter the amount of disks sold: 50
Enter the amount of disks sold: 34
Enter the quantity of sold discs: 89
Enter the amount of disks sold: 90

The largest number of albums sold was: 90
The largest amount of sales was made day: 5/MAR

  • 1

    Sorry for the lack of identação, first time I publish, happened of I copy and paste, and I did not pay attention to that fact. Thank you so much for helping me, I have some very grotesque difficulties in applying logic, and the way you wrote your code with the comments, were perfect.

  • I registered here yesterday, also I did not know how to move right. I edited the post some 10x hehehe, but very good to know that managed to understand friend. I hope I’ve helped and good luck with your projects!

  • Thank you very much brother, helped and very much !

Browser other questions tagged

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