我的程序的目的是让用户输入捐赠目标。当达到这个数字目标时,我的程序应该停止。在向用户询问目标后,我的程序会询问捐赠金额,每个捐赠金额在达到目标之前相互相加。最后,在输入多个捐赠金额后,它应该加起来达到目标。
goal = ''
userDonation = ''
totalAmount = 0
userCashList = []
#asking for end goal
goal = int(input("Some Charity Donation Tracker... Please enter your goal: "))
print("Current Amount: ", totalAmount)
while totalAmount != goal:
userDonation = int(input("Please Enter Donation Amount: "))
print("Current Amount: ", userDonation + totalAmount)
userCashList.append(totalAmount)
for cash in userCashList:
totalAmount += userDonation
if userDonation == goal:
print("You have met your goal!")
else:
userCashList.append(userDonation)
输出:
Some Charity Donation Tracker... Please enter your goal: 100
Current Amount: 0
Please Enter Donation Amount: 200
Current Amount: 200
Please Enter Donation Amount: 300
Current Amount: 500
Please Enter Donation Amount: 200
Current Amount: 1300
Please Enter Donation Amount:
我的捐款额在增加,但为什么不能停在100呢?
我不知道为什么您需要userCashList
数组,而到目前为止您已经有了totalAmount
来显示捐赠。不管怎样,我已经对你的代码做了几次更改,它似乎工作得很好!
首先,只有当totalAmount
与goal
不完全一样时,while totalAmount != goal
才会运行。这意味着,如果你击中了目标,即用户捐赠了100,目标为100,代码只会通过while循环,而不会表明你已经通过了目标。
这是新代码:
goal = ''
userDonation = ''
totalAmount = 0
#asking for end goal
goal = int(input("Some Charity Donation Tracker... Please enter your goal: "))
print("Current Amount: ", totalAmount)
while True:
userDonation = int(input("Please Enter Donation Amount: "))
totalAmount = totalAmount + userDonation
print("Current Amount: ", totalAmount)
if totalAmount >= goal:
print("You have met your goal!")
break # Break out of the loop when the goal is met
while
循环不是在总数不等于目标时才运行,而是永远运行。if
语句会停止这一操作,如果总和等于或大于目标,则会检查每个增量。如果是,请通知用户并中断循环,从而停止脚本。
我刚开始回答SO问题,所以我希望这能有所帮助!:(
当条件为
!=
时,这意味着只有当total_amount
等于目标时循环才会停止(如果total_amount
大于目标(,而不是仅使用<
(小于操作员(
您的代码,但有所改进:
total_amount = 0
# asking for end goal
goal = int(input("Some Charity Donation Tracker... Please enter your goal: "))
print("Current Amount:", total_amount)
while total_amount < goal:
user_donation = int(input("Please Enter Donation Amount:"))
total_amount += user_donation
print("Current Amount: ", total_amount)
else:
print("You have met your goal!")
我试图用较少的变量修改您的代码。请检查这是否是解决您问题的更好方法。
userDonation = ''
userCashList = []
#asking for end goal
goal = int(input("Some Charity Donation Tracker... Please enter your goal: "))
print("Current Amount: ", sum(userCashList))
while sum(userCashList) <= goal:
userDonation = int(input("Please Enter Donation Amount: "))
userCashList.append(userDonation)
print("Current Amount: ", sum(userCashList))
if sum(userCashList) >= goal:
print("You have met your goal!")
break
我在这里看到了几个问题:
错误的停车条件
你目前的条件是totalAmount != goal
,但问题是要停止,你的totalAmount
必须与既定目标完全相同,如果目标被totalAmount
超过,但永远不会等于它,那么循环将永远不会停止。这就是为什么while循环中的停止条件应该是totalAmount < goal
。
totalAmount的增量错误
在这个for循环中,您的总金额增加过多:
for cash in userCashList:
totalAmount += userDonation
这个for循环完全是不必要的,并且会影响应用程序的正确功能。
这就是我建议你的代码应该是:
goal = ''
userDonation = ''
totalAmount = 0
userCashList = []
#asking for end goal
goal = int(input("Some Charity Donation Tracker... Please enter your goal: "))
print("Current Amount: ", totalAmount)
while totalAmount < goal:
userDonation = int(input("Please Enter Donation Amount: "))
print("Current Amount: ", userDonation + totalAmount)
userCashList.append(userDonation)
totalAmount += userDonation
if totalAmount >= goal:
print("You have met your goal!")
正如您在这里看到的,没有必要使用break,因为循环条件是足够的。此外,我发现userCashList是捐款的某种记录。