简体   繁体   中英

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

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM