-1

I want to print it starting from "Francisco" and ending with "Antonio", but keeping text with the numbers increasing, I really don't know where to go with this, I'm very new, thank you for helping!

list = ['Francisco', 'Marcelle', 'Olivia', 'José', 'Guilherme', 'Maria', 'Arthur', 'Lucas', 'Lurdes', 'Antonio']    

for x in range(1,11) :
    print(list[x] "your number on the list is, " + str(x) + ".") 

In the last line I found how to keep text, add increasing number but I don't know how to print different element with range, for, I started programming not long

Example: Francisco, your number is 1, Marcelle, your number is 2, Antonio, your number is 10

  • Use `enumerate`, as shown in [Accessing the index in 'for' loops?](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – mkrieger1 Aug 09 '21 at 14:52

5 Answers5

0

Is the below what you are looking for?

names = ['Francisco', 'Marcelle', 'Olivia', 'José', 'Guilherme', 'Maria', 'Arthur', 'Lucas', 'Lurdes', 'Antonio']
for idx, name in enumerate(names, 1):
    print(f'{name}, your number is {idx}')

output

Francisco, your number is 1
Marcelle, your number is 2
Olivia, your number is 3
José, your number is 4
Guilherme, your number is 5
Maria, your number is 6
Arthur, your number is 7
Lucas, your number is 8
Lurdes, your number is 9
Antonio, your number is 10
balderman
  • 22,927
  • 7
  • 34
  • 52
0

I think this is what you want to print: Francisco your number on the list is: 1 Marcelle your number on the list is: 2 Olivia your number on the list is: 3 José your number on the list is: 4 Guilherme your number on the list is: 5 Maria your number on the list is: 6 Arthur your number on the list is: 7 Lucas your number on the list is: 8 Lurdes your number on the list is: 9 Antonio your number on the list is: 10 If is it the case then you can use this code:

L = ['Francisco', 'Marcelle', 'Olivia', 'José', 'Guilherme', 'Maria', 
   'Arthur', 'Lucas', 'Lurdes', 'Antonio']    

for i in range (len(L)):
print(L[i],"your number on the list is:",i+1)
0

You were close to the correct code which is

list = ['Francisco', 'Marcelle', 'Olivia', 'José', 'Guilherme', 'Maria', 'Arthur', 'Lucas', 'Lurdes', 'Antonio']    

for x in range(0,len(list)) :
    print(list[x] + " your number on the list is " + str(x) + ".") 

Modifications from yours

What you did wrong :

  • you forgot the + of concatenation in list[x] + " your number on the list is "
  • you started at the index 1 in range, but lists start at index 0

What I improved :

  • in the range, if you don't know in advance the length of list, use len(list)

Have fun discovering Python and practising! :)

0

You could do that using enumerate() with start argument set to 1 (Since you need to start counting from 1).

Note: Don't use list as variable names. That's a python reserved word.

lst = ['Francisco', 'Marcelle', 'Olivia', 'José', 'Guilherme', 'Maria', 'Arthur', 'Lucas', 'Lurdes', 'Antonio']
for i,v in enumerate(lst,start=1):
    print(f'{v}, your number is {i}')
Francisco, your number is 1
Marcelle, your number is 2
Olivia, your number is 3
José, your number is 4
Guilherme, your number is 5
Maria, your number is 6
Arthur, your number is 7
Lucas, your number is 8
Lurdes, your number is 9
Antonio, your number is 10
Ram
  • 4,724
  • 2
  • 14
  • 22
0

There are a couple of mistakes that you make here, some of the more code breaking than others. I'll list them here.

  1. First of don't just the list name as a variable name, e.g. change list to list_of_names. The list namespace is already used by python, as the list class see here.
  2. list start from 0. You start from 1 and run to 11. Therefore you will list index will run of of range. Change the for loop to either
for i, name in enumerate(list_of_names):

Here i already has the number and name has the name.

You could use that in the print statement.

or

for i in range(len(list_of_names)):
  1. The print statement is not formatted correctly, one way to do better formatting is by using the f-string.
print(f"{list[x]} your number on the list is, {str(x)}.")

all of this will result in the following code piece added together.

list_of_names = ['Francisco', 'Marcelle', 'Olivia', 'José', 'Guilherme', 'Maria', 'Arthur', 'Lucas', 'Lurdes', 'Antonio']
for x in range(len(list_of_names)):
     print(f"{list[x]} your number on the list is, {str(x+1)}.")

Good luck learning!

Casper
  • 1
  • 5