Walter Johnson
Walter Johnson

Reputation: 13

Proper Syntax for a for-loop?

Here's my code

days = 10
total = 0

for currentday in range(1, days +1):
 > bugs = int(input("how many bugs were collected on day"+ str(currentday),": ")
 > total += bugs
print("The total number of bugs caught by the end of all",days,"days was",total)

I keep experiencing syntax errors on line 6 or where the "total += bugs" is. I can't even identify what the error is so I need help in finding what's wrong with it

Upvotes: 0

Views: 58

Answers (2)

Maxijazz
Maxijazz

Reputation: 185

The '>'s aren't helping, but those were probably added when you copy-pasted your code. The main problem is simply a forgotten bracket on the line above. It ends ": ") when it should end ": "))

Furthermore, you can't just put commas to add strings and integers to make a string. You have to convert the integer variables days and total to strings first.

Here is your code with the errors resolved:

days = 10
total = 0

for currentday in range(1, days + 1):
    bugs = int(input("how many bugs were collected on day" + str(currentday) + ": "))
    total += bugs

print("The total number of bugs caught by the end of all ", str(days), " days was ", str(total))

Upvotes: 0

Evorage
Evorage

Reputation: 503

days = 10
total = 0

for currentday in range(1, days +1):
    bugs = int(input(f"how many bugs were collected on day {currentday}: "))
    total += bugs
print("The total number of bugs caught by the end of all",days,"days was",total)

A: Your missing a bracket ")" on the end of line 6.

B: You cant put variables like that into an input, use the formatting option as seen above

Upvotes: 1

Related Questions