6 Replies - 247 Views - Last Post: 11 January 2019 - 10:03 AM Rate Topic: -----

#1 dragu_stelian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-June 15

input to array

Posted 11 January 2019 - 01:33 AM

I have the next code:

tab = [10, 11, 12, 13, 14, 15]
print()
print("Print array using FOR loop")
for i in range(len(tab)):
    print("tab[{}] = {}".format(i, tab[i]))



I want to input values from keyboard like this:

tab[0] = input value
tab[1] = input value
...
...
tab[5] = input value



In C++ looks like this

for (int i=0; i<5; i++)
{
    cout << "\n tab[" << i <<"] = ";
    cin >> tab[i];
}



Is This A Good Question/Topic? 0
  • +

Replies To: input to array

#2 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6743
  • View blog
  • Posts: 27,758
  • Joined: 12-December 12

Re: input to array

Posted 11 January 2019 - 02:09 AM

Okay, so what have you discovered or attempted? Else what is your question?

Also, please use a more descriptive title than just the single word 'array'. I have changed it for you.
Was This Post Helpful? 0
  • +
  • -

#3 dragu_stelian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-June 15

Re: input to array

Posted 11 January 2019 - 07:30 AM

I tried this, but it does not work.

array = []
dim = int(input("Enter size of the array: "))

for i in range(0, dim):
    tab = int(input("array[{}] = "), (format(i)))
    array.append(tab)

for i in range(len(array)):
    print("array[{}] = {}".format(i, array[i]))


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6743
  • View blog
  • Posts: 27,758
  • Joined: 12-December 12

Re: input to array

Posted 11 January 2019 - 07:48 AM

Do you receive errors? If so, provide the error details.
Was This Post Helpful? 0
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 507
  • View blog
  • Posts: 1,598
  • Joined: 27-December 13

Re: input to array

Posted 11 January 2019 - 08:41 AM

As andrewsw says: Always provide error messages, they are often most helpful.

Line 5 in your code has wrong syntax, it should be:
tab = int(input("array[{}] = ".format(i)))

You might benefit from reading up on string formating...
Was This Post Helpful? 0
  • +
  • -

#6 dragu_stelian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-June 15

Re: input to array

Posted 11 January 2019 - 09:07 AM

Noe it works fine, thank you DK3250!
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,832
  • Joined: 13-June 14

Re: input to array

Posted 11 January 2019 - 10:03 AM

FWIW:

1. When you call range on line 4, you only need to pass the ending value - the starting value will be 0 by default.

2. You can use the enumerate function to give you pairs of index and value:

>>> for i, v in enumerate(["foo", "bar", "baz"]):
...     "{}: {}".format(i, v)
... 
'0: foo'
'1: bar'
'2: baz'


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1