0
I wanted to take a variable and store different values. In this case, it would be 7 ages, how to do?
cont = 0
anos = 0
for c in range(1, 8):
cont += 1
anos = int(input('Em que ano a {} pessoa nasceu? '))
0
I wanted to take a variable and store different values. In this case, it would be 7 ages, how to do?
cont = 0
anos = 0
for c in range(1, 8):
cont += 1
anos = int(input('Em que ano a {} pessoa nasceu? '))
4
You would need to create a list
to do so. And use .append()
to add values to the list.
cont = []
for c in range(1,8):
cont.append(c)
print(c)
Thanks Augusto :)
2
Use lists (although similar to the array of other languages, the documentation calls the list):
idades = [] # lista vazia
for i in range(1, 8):
idades.append(int(input(f'Em que ano a {i}ª pessoa nasceu?')))
Note that you don’t need the variable cont
, use your own i
which is being used by for
(I think you missed that in your loop).
Another option is to create the list at once, using comprehensilist on:
idades = [ int(input(f'Em que ano a {i}ª pessoa nasceu?')) for i in range(1, 8) ]
Intendi, there for example if I want to print on the screen or make some sum with these variables I would need to put a number inside brackets that represent each person. EX: age[2] if I want to use the second stored value?
@Gabrieldasilvasantos In fact the indexes start at zero, so idades[0]
is the first, idades[1]
is the second, etc. And there’s a lot you can do with lists, I suggest you read this tutorial carefully: https://realpython.com/python-lists-tuples/
Obg by help. I will give a read and study more about. tmj ^-^
Browser other questions tagged python variables
You are not signed in. Login or sign up in order to post.
what you want to use is called array. Here are some examples: https://answall.com/questions/462627/impress%C3%a3o-de-matrices-in-python
– Ricardo Pontual
intendi, then in case I can store different values with this matrix right, I’m beginner do not understand much yet
– Gabriel da Silva Santos
and access the values by the "index" :)
– Ricardo Pontual