简体   繁体   English

如果 Python 中的输入为负数,如何结束循环?

[英]How to end loop if input is negative in Python?

I am writing a program for an assignment that has the following conditions:我正在为具有以下条件的作业编写程序:

  1. 12 bars per case, represented by bars_per_case每个案例 12 个柱,由bars_per_case表示

  2. A price of 1 per bar: price_per_bar每条 1 的价格: price_per_bar

  3. A case cost of 8: cost_per_case案例成本为 8: cost_per_case

  4. A 10% cut: SGA削减 10%: SGA

The program should ask the user how many bars were sold and calculate the SGA amount and total net gain cheer_proceeds .程序应该询问用户卖出了多少条,并计算SGA数量和总净收益cheer_proceeds The program should have a different message depending on cheer_proceeds : "Congratulations ..." when cheer_proceeds >= 500 or "Sorry ..." when cheer_proceeds < 500 .该程序应根据cheer_proceeds不同的消息:当cheer_proceeds >= 500时为"Congratulations ..."或当cheer_proceeds < 500时为"Sorry ..."

The program should NOT accept a negative number of bars.程序不应接受负数的柱线。 If a negative number is entered, the program should NOT move forward to calculate totals until valid data is entered.如果输入负数,则程序在输入有效数据之前不应继续计算总数。

My problems are:我的问题是:

  1. When I use the while loop, the last lines the if cheer_proceeds... keeps looping当我使用while循环时,最后if cheer_proceeds...if cheer_proceeds...不断循环
  2. When entering a negative number the code still continues输入负数时,代码仍然继续

Where would I insert a break in order to stop the loop?我应该在哪里插入一个break以停止循环?

My code:我的代码:

    # program computes money cheerleaders raised
    
    cost_per_case = 8  # $8.00
    bars_per_case = 12
    price_per_bar = 1  # $1.00
    SGA = 0.10  # 10 percent earnings
    cheergoal = 500  # $500 is their goal
    
    # ask user how many bars were sold
    barsSold = int(input('Bars sold: '))
    
    # calculate and display proceeds
    while barsSold >= 0:
        total_cost_candy = (barsSold / 12) / 8
        total_profit = (barsSold * price_per_bar) - total_cost_candy
        sga_proceeds = total_profit * 0.1
        cheer_proceeds = total_profit - sga_proceeds
    
    # display proceeds and if goal was met or not
    print(sga_proceeds, cheer_proceeds)
    if cheer_proceeds >= 500:
        print('Congrats! You raised $500 or mroe!')
    else:
        print('Sorry! You did not meet your goal of $500!')

When the user enters an amount, just loop until the amount is non-negative:当用户输入金额时,循环直到金额为非负数:

# program computes money cheerleaders raised

cost_per_case = 8  # $8.00
bars_per_case = 12
price_per_bar = 1  # $1.00
SGA = 0.10  # 10 percent earnings
cheergoal = 500  # $500 is their goal

# ask user how many bars were sold
barsSold = int(input('Bars sold: '))

# calculate and display proceeds
while barsSold < 0:   # loop until valid number
    print('invalid amount')
    barsSold = int(input('Bars sold: '))
    
total_cost_candy = (barsSold / 12) / 8
total_profit = (barsSold * price_per_bar) - total_cost_candy
sga_proceeds = total_profit * 0.1
cheer_proceeds = total_profit - sga_proceeds

# display proceeds and if goal was met or not
print(sga_proceeds, cheer_proceeds)
if cheer_proceeds >= 500:
    print('Congrats! You raised $500 or mroe!')
else:
    print('Sorry! You did not meet your goal of $500!')

What makes the same code run over and over within the same program?是什么让相同的代码在同一个程序中一遍又一遍地运行? Loops do.循环。 So anytime your problem description includes repeating something (like asking a question until a positive number is given), you'll want a loop.因此,无论何时您的问题描述包括重复某些内容(例如在给出正数之前提出问题),您都需要一个循环。

Any time you aren't doing something over and over (like calculating the bars sold), you don't want a loop.任何时候你不一遍又一遍地做某事(比如计算出售的条形),你不想要一个循环。

To directly answer your questions...直接回答你的问题...

  1. I'm not sure why if cheer_proceeds would keep looping as that's outside a loop.我不确定为什么if cheer_proceeds会继续循环,因为那是在循环之外。 Did you provide all of the code?你提供了所有的代码吗?
  2. The code moves forward because it's not in a loop.代码向前移动,因为它不在循环中。
  3. You don't want a break.你不想休息。 There shouldn't be a loop there.那里不应该有循环。

while loop is not useful here, use nested if condition. while 循环在这里没有用,请使用嵌套的 if 条件。

cost_per_case = 8  # $8.00
bars_per_case = 12
price_per_bar = 1  # $1.00
SGA = 0.10  # 10 percent earnings
cheergoal = 500  # $500 is their goal
    
    # ask user how many bars were sold
barsSold = int(input('Bars sold: '))
    
    # calculate and display proceeds
if barsSold > 0:
    total_cost_candy = (barsSold / 12) / 8
    total_profit = (barsSold * price_per_bar) - total_cost_candy
    sga_proceeds = total_profit * 0.1
    cheer_proceeds = total_profit - sga_proceeds

    # display proceeds and if goal was met or not
    
    if cheer_proceeds >= 500:
        print(sga_proceeds, cheer_proceeds)
        print('Congrats! You raised $500 or mroe!')
    else:
        print(sga_proceeds, cheer_proceeds)
        print('Sorry! You did not meet your goal of $500!')    
else:
    print('Invalid Input')
    

I believe you have some issues with indentation as your code currently stands.我相信您的代码目前存在一些缩进问题。 I had to fudge with it a bit to get it to run at all.为了让它运行起来,我不得不对它稍加改动。 As the other user mentioned this does not need to be done with a while loop.正如其他用户提到的,这不需要用 while 循环来完成。 BUT, it can be done this way.但是,它可以通过这种方式完成。

  1. when i use the while loop the last lines (the if cheer_proceeds.....) keeps looping.当我使用 while 循环时,最后几行(如果cheer_proceeds .....)不断循环。
  • Yes, if you have a while loop, as long as the condition is met the loop will continue forever.是的,如果你有一个 while 循环,只要满足条件,循环就会永远持续下去。 You need the break in there or the condition needs to be met for the loop to exit您需要在那里中断或需要满足条件才能退出循环
  1. when entering a negative number the code still tries to move foward.. (how to resolve this ?)输入负数时,代码仍会尝试向前移动..(如何解决此问题?)
  • The issue here is the ordering of your code.这里的问题是代码的顺序。 If you ask for barsSold before the loop, and the user inputs a not accepted option, the program will not ask them again to input an appropriate answer.如果您在循环之前询问 barSold,并且用户输入了未接受的选项,则程序将不会再次要求他们输入适当的答案。 Not only that, but you have not put any conditional statement to tell the program what to do if the user inputs a negative answer.不仅如此,您还没有放置任何条件语句来告诉程序如果用户输入否定答案该怎么办。
  1. where would i insert a break in order to stop said loop?我会在哪里插入一个中断以停止所述循环?
  • the break should be inserted in a conditional statement (the if loop), so that you can get the loop to break at a specific point (ie when someone enters a non-negative answer)中断应插入条件语句(if 循环)中,以便您可以使循环在特定点中断(即当有人输入非否定答案时)

Here is a working version of the code这是代码的工作版本

# program computes money cheerleaders raised
cost_per_case = 8  # $8.00
bars_per_case = 12
price_per_bar = 1  # $1.00
SGA = 0.10  # 10 percent earnings
cheergoal = 500  # $500 is their goal


# calculate and display proceeds
while True:
    # ask user how many bars were sold
    barsSold = int(input('Bars sold: '))
    if barsSold >=0:
        total_cost_candy = (barsSold / 12) / 8
        total_profit = (barsSold * price_per_bar) - total_cost_candy
        sga_proceeds = total_profit * 0.1
        cheer_proceeds = total_profit - sga_proceeds
        print(sga_proceeds, cheer_proceeds)
        
        if cheer_proceeds >= 500:
            print('Congrats! You raised $500 or mroe!')
        else:
            print('Sorry! You did not meet your goal of $500!')

        break 
    else:
        print("That is not a suitable input. Please try again.")

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

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