Is there another way to access the index of a Python list?

Asked

Viewed 86 times

0

I want to know:

  • There is another way to access the index in question, where I am using in print()?
  • Guarded in 'dice' the respective values, but because they are decimal, I’m having difficulty reporting to the user.
  • Although almost ready, if possible I want suggestions for improvement.

Follows the script:

print('\n') 
dados = []
for index in range(2):
    print('\n') #   Espaço linha
nome = input('None do Atleta: ')
nome.capitalize()
if nome < '.':
    print('Reenicie o sistema, digitando o nome do atleta')
    break
print('\n') #   E2
distância = input('Distancia do salto: ')
dados.append(distância)

print(f'Atleta: {nome}\n')
print(f'Primeiro salto: {distância[0:2]}')
print(f'Segundo salto: {distância[2:4]}')
print(f'Dados informados: {dados[0:]}')
  • 3

    It has how to place a sample of data, or a specification of the data, with which to feed the algorithm?

  • 4

    Just for the record that the operation being performed is not indexing per se, but a slicing.

  • @Augustovasques, I will modify. Thank you.

  • 3

    Daniel, can you give us an example of what the program entry will be? And better explain what the condition should be if nome < '.'?

  • @Woss, YES, of course. The entrance asks:

  • 3

    Ask what? I guess you haven’t finished the comment.

  • 3

    That one break within the if makes a mistake, since break can only be inside a loop (and in the case, it is not). It is also unclear what exactly you are trying to do, because the list dados will only have one element, and by doing distancia[0:2] you are accessing pieces of the string that was typed. Anyway, everything is very confusing...

  • 1

    @Woss, YES, of course. The entry asks: the name of the athlete, later, if the name is less than '.' it will stop. That is, if the user does not enter the name of the athlete, there is no continuity. This was the way I thought for this situation. In processing, pg ira takes the typed notes, e.g., 8.4, or 3.6 (decimals) and places them in dice, then I have to return these values of note pro usuario as it is there in the print, later, it informs: first note: 3.4; second note: 8.6, so on.

  • 2

    @Danielalvesdebarros, take a look in that print. Note that I modified the recoil of the code and changed if nome < '.': for if nome.strip() == "": . But I’m still having a hard time understanding what you’re trying to do, because for me I would make a dictionary of jumps and each jump would treat it as a numerical value.

  • 2

    "if the user does not enter the athlete’s name" - test if the name is less than . nay That’s what if I type ; will be considered greater than ., for example. You would have to test whether the name is empty, as already suggested by Augusto above.

  • 2

    And it makes no sense to type all the notes at once and save everything in the same string to then pick up pieces of it, the best is to have a list of notes (again, as already suggested)

  • @Augustovasques, initially I put this condition to name as 'if name == ()', but obviously, as already mentioned by you, does not. Thanks for the neat method sent 'strip'/placed track shared. I could do yes with dicio, however this is a question. But I don’t know if it will be possible, since I’m having great difficulty finding the list’s Index, ex 'print(f'First jump {distance[0:2]}'). However because it comes to decimals becomes difficult

  • @hkotsubo, (interesting username that your rsrs), my dear, thank you. I already reported your note pro program and ranked you.

  • From what I understand you’re trying to do something like this: https://ideone.com/mUAfIU

Show 9 more comments
No answers

Browser other questions tagged

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