Sort as typed (python)

Asked

Viewed 145 times

0

with a problem that would be the following, the problem asks to order the numbers in the order you typed them and then do in reverse order, someone can give me a light, the code is more or less this:

ps: not in list.

this cont is pq tbm has q show the media and how many were typed.

(i am using while true, pq in problem it to terminate qnd type a negative number)

cont = 0
while True:
    notas = float(input("Informe a nota: "))
    if notas == -1:
        break
    cont += 1
print(cont)
  • You can use an array?

  • Not really, because we’re not there yet

  • 1

    You’re not doing anything with notas. You can’t show the bills if you don’t store them. I don’t see how to do this without a list, except creating several variables beforehand, one for each note (but then you don’t know how many notes will be typed)

  • I’m also finding it strange to do this without list, but well, I think I’ll do with list with list I just did WITH LIST LOOKS LIKE THIS: cont = sum = media = 0 order = [] inverse = [] while True: notes = float(input("Enter note: ")) if notes == -1: break order.append(notes) inverse.append(notes) cont += 1 sum += notes media = sum / cont print(cont) print(order) (inverse[:print-1]) print(sum) print(media)

1 answer

0

It is really something quite artificial to impose an exercise of these and "probir to use lists" why "not yet arrived in this part of the matter".

If you happen to have "as part of the subject", the string manipulation and string methods, one way to do this is to concatenate all the values typed into a string, with a separator, and use the "split" method to get back the values.

But the "for" itself in Python naturally runs through elements of a list - which will be the return of the "split" function. I consider that this way of teaching is wrong. If you want, feel free to put this teacher in touch with me.

cont = 0
valores = ""
while True:
    nota_txt = input("Informe a nota: ")
    if float(notas_txt) == -1:
        break
    valores += notas_txt + ","
    cont += 1
for valor in valores[:-1].split(","):
    print(valor)
total = 0
for valor in reversed(valores[:-1].split(",")):
    print(valor)
    total += float(valor)

media = total / valores.count(",")
print("media:", media)

Browser other questions tagged

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