-2

How do you refer to an integer inside a string, using python? I am completely new to coding and I am trying to do this bug collection exercise where the user will input the number of bugs collected each day for a week and be presented the total number of bugs collected at the end of the week.

This is the code i have so far.

totalBugs = 0.0
day = 1

for day in range(7):
    bugsToday = input('How many bugs did you get on day', day,'?')
    totalBugs = totalBugs + bugsToday

print 'You\'ve collected ', totalBugs, ' bugs.'

So i'm trying to get the bugsToday prompt inside the loop to ask the user "How many bugs did you collect on day 1?" "How many bugs did you collect on day 2?" And so forth.

How do I do that?

  • You want to "read from user input". Check out: http://stackoverflow.com/questions/3345202/python-getting-user-input – doublesharp Jun 21 '15 at 02:19
  • possible duplicate of [How to convert strings into integers in python?](http://stackoverflow.com/questions/642154/how-to-convert-strings-into-integers-in-python) – Paul Jun 21 '15 at 02:22
  • `s="34"` `n=int(s)` `print n+1` `35` – Paul Jun 21 '15 at 02:23
  • 1
    Unclear what the problem is. Are you having trouble doing `some_string + some_int`? Are you having trouble requesting the user's input? Are you asking about the `for` loop? Kind of no-matter-what this is a duplicate question, but I'm not sure where to point it at – Adam Smith Jun 21 '15 at 02:23

3 Answers3

2

Personally I really like format(). You could write code as this:

totalBugs = 0
for day in range(1, 8):
   bugsToday = raw_input('How many bugs did you get on day {} ?'.format(day))
   totalBugs += int(bugsToday)

print 'You\'ve collected {} bugs.'.format(totalBugs)

range(1, 8) goes through day = 1 to day = 7, if that's what you want to do.

Atrotors
  • 765
  • 2
  • 7
  • 25
1

You may try

...
for day in range(7):
    bugsToday = input('How many bugs did you get on day %d ?' % day)
    totalBugs = totalBugs + bugsToday
...
0

The way I would do it is as follows.

total_bugs = 0 #assuming you can't get half bugs, so we don't need a float

for day in xrange(1, 8): #you don't need to declare day outside of the loop, it's declarable in the  for loop itself, though can't be refernced outside the loop.
    bugs_today = int(raw_input("How many  bugs did you collect on day %d" % day)) #two things here, stick to raw_input for security reasons, and also read up on string formatting, which is what I think answers your question. that's the whole %d nonsense. 
    total_bugs += bugs_today #this is shorthand notation for total_bugs = total_bugs + bugs_today.

print total_bugs

To read up on string formatting: http://www.learnpython.org/en/String_Formatting

Article I wrote about raw_input vs. input for security purposes if you're interested in that: https://medium.com/@GallegoDor/python-exploitation-1-input-ac10d3f4491f

From the Python Docs:

CPython implementation detail: If s and t are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form s = s + t or s += t. When applicable, this optimization makes quadratic run-time much less likely. This optimization is both version and implementation dependent. For performance sensitive code, it is preferable to use the str.join() method which assures consistent linear concatenation performance across versions and implementations.

Programming can seem overwhelming at first, just stick to it you won't regret it. Good luck my friend!

Dor-Ron
  • 1,787
  • 3
  • 15
  • 27
  • Thank you!!! This was exactly what i was looking for!!! I'll be sure to remember the %d. – Benjamin Beau Davis Jun 21 '15 at 02:46
  • No problem, glad to help. A few things I might've missed, xrange is faster than range so I use that by habit. (1, 8) 1-7 instead of 0-6 which I though was nicer, but up to you. and I used int() on raw_input because raw_input returns a string, and we want an int type. Also note that %d is for ints, %s it for strings, %f for floats etc. you might find the .format method easier. – Dor-Ron Jun 21 '15 at 02:48