我正在運行這個回圈代碼并且它正在創建一個錯誤,我找不到它的問題
print("""\
This program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.\n""")
AmountSpent = 0
Budget = 0
numMonths = int(input("Enter the number of months you would like to monitor:"))
while numMonths<0:
print("\nNegative value detected!")
numMonths = int(input("Enter the number of months you would like to monitor"))
for month in range(1, numMonths 1):
print("\n=====================================")
AmountBudgeted = float(input("Enter amount budgeted for month " month ":"))
while AmountBudgeted<0:
print("Negative value detected!")
AmountBudgeted = float(input("Enter amount budgeted for month " month ":"))
AmountSpent = float(input("Enter amount spent for month " month ":"))
while AmountSpent<0:
print("Negative value detected!")
AmountSpent = float(input("Enter amount spent for month " month ":"))
if AmountSpent <= AmountBudgeted:
underBy = AmountBudgeted - AmountSpent
print("Under budget by " underBy)
else:
overBy = AmountSpent - AmountBudgeted
print("Over budget by " overBy)
if month == "1":
print(f'your budget is {AmountBudgeted}.')
關于我為什么會收到此錯誤的任何想法?我試圖自己弄清楚,但我不知道為什么它是錯誤的
uj5u.com熱心網友回復:
你的問題是for回圈變數month是一個整數,所以你不能在不先轉換它的情況下將它連接到一個字串。解決這個問題的最簡單方法是改用格式字串。
例如,這一行:
AmountBudgeted = float(input("Enter amount budgeted for month " month ":"))
應改為:
float(input(f"Enter amount budgeted for month {month}:"))
(如果你使用的是舊版本的 Python,你會改寫float(input("Enter amount budgeted for month {month}:".format(month=month)))。)
如果您很難改用串聯,則訣竅是month首先使用str(month).
希望有幫助!
uj5u.com熱心網友回復:
字串和數字資料型別存在問題。這是您的解決方案:
print("""\
This program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.\n""")
AmountSpent = 0
Budget = 0
numMonths = int(input("Enter the number of months you would like to monitor:"))
while numMonths<0:
print("\nNegative value detected!")
numMonths = int(input("Enter the number of months you would like to monitor"))
for month in range(1, numMonths 1):
print("\n=====================================")
AmountBudgeted = float(input("Enter amount budgeted for month " str(month) ":"))
while AmountBudgeted<0:
print("Negative value detected!")
AmountBudgeted = float(input("Enter amount budgeted for month " str(month) ":"))
AmountSpent = float(input("Enter amount spent for month " str(month) ":"))
while AmountSpent<0:
print("Negative value detected!")
AmountSpent = float(input("Enter amount spent for month " str(month) ":"))
if AmountSpent <= AmountBudgeted:
underBy = AmountBudgeted - AmountSpent
print("Under budget by " str(underBy))
else:
overBy = AmountSpent - AmountBudgeted
print("Over budget by " str(overBy))
if month == "1":
print(f'your budget is {AmountBudgeted}.')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/532897.html
上一篇:睡眠時長條形圖
