3 Replies - 583 Views - Last Post: 20 September 2012 - 09:45 PM Rate Topic: -----

#1 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 19-September 12

Noob question about incrementing variable inside input statement

Posted 19 September 2012 - 03:51 PM

Hello all! I have a program where I have a for loop, and inside of the foor loop is an input statement that allows the user to enter a number. For example,

for i in range(4):
    cars = input("Please enter car number 1")



However, what I am trying to do is have it so each time the for loop runs, it increases the car number. I.E. "please enter car number 1" and then "please enter car number 2" the next time around. I tried having cars = input("Please enter car number %d") %i but that didn't seem to work. I just can't figure out how to do it. Any help would be greatly appreciated!

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Noob question about incrementing variable inside input statement

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Noob question about incrementing variable inside input statement

Posted 19 September 2012 - 04:03 PM

The i is 0 indexed (so i goes from 0 to 3) all you need is:
for i in range(4):
   cars = input("Please enter car number " + str(i + 1))


Which adds the string value of i+1 to the input string. Hope that helps :)

This post has been edited by Simown: 19 September 2012 - 04:05 PM

Was This Post Helpful? 2
  • +
  • -

#3 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 19-September 12

Re: Noob question about incrementing variable inside input statement

Posted 19 September 2012 - 04:13 PM

Yes that is exactly what I wanted! Thank you very much! :)
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Noob question about incrementing variable inside input statement

Posted 20 September 2012 - 09:45 PM

Another possibility is:
for i in range(1,5):#start at 1, go up to but not including 5
   cars = input("Please enter car number " + str(i))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1