簡體   English   中英

for 循環的正確語法?

[英]Proper Syntax for a for-loop?

這是我的代碼

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)

我一直在第 6 行或“總 += 錯誤”所在的地方遇到語法錯誤。 我什至無法確定錯誤是什么,所以我需要幫助來找出錯誤所在

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)

答:您在第 6 行末尾缺少括號“)”。

B:您不能將這樣的變量放入輸入中,請使用如上所示的格式選項

'>'s 沒有幫助,但那些可能是在您復制粘貼代碼時添加的。 主要問題只是上面一行中被遺忘的括號。 它以": ")結束時結束": "))

此外,你不能只用逗號來添加字符串和整數來生成字符串。 您必須首先將整數變量daystotal轉換為字符串。

這是您已解決錯誤的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM