当达到用户的数字目标时,我的程序如何停止 运行?

How can my program stop running when a user's number goal is met?

我的程序的目的是让用户输入捐赠目标。当达到该数字目标时,我的程序应该停止。在向用户询问目标后,我的程序会询问捐赠金额,每个捐赠金额相互叠加以实现目标。最后,输入多次捐款金额后,加起来应该就是目标了。

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 不完全是 goalwhile 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 问题,希望对您有所帮助! :)

when your condition != this means the loop will stop only if total_amount is equal the goal (your program enter infinite loop if total_amount greater than goal), instead of just use < (less than operator).

你的代码改进了:

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

我发现这里有几个问题:

while 中错误的停止条件

你目前的状态是totalAmount != goal,但问题是要停止,你的totalAmount必须与既定目标完全一致,如果超过了目标,totalAmount 但永远不会等于它那么循环永远不会 stop.This 这就是为什么 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!")

正如你在这里看到的,不需要使用中断,循环条件就足够了。我还认为 userCashList 是某种捐赠记录。