繁体   English   中英

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

[英]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)

Output:

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?

当您已经拥有totalAmount来显示到目前为止的捐款时,我不确定为什么还需要userCashList数组。 无论如何,我对您的代码进行了几处更改,它似乎工作正常!

首先, while totalAmount != goal仅在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 的增量错误

您的 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 是某种捐赠记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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