[英]How to end loop if input is negative in Python?
I am writing a program for an assignment that has the following conditions:我正在为具有以下条件的作业编写程序:
12 bars per case, represented by bars_per_case
每个案例 12 个柱,由
bars_per_case
表示
A price of 1 per bar: price_per_bar
每条 1 的价格:
price_per_bar
A case cost of 8: cost_per_case
案例成本为 8:
cost_per_case
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:我的问题是:
while
loop, the last lines the if cheer_proceeds...
keeps loopingwhile
循环时,最后if cheer_proceeds...
行if cheer_proceeds...
不断循环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...直接回答你的问题...
if cheer_proceeds
would keep looping as that's outside a loop.if cheer_proceeds
会继续循环,因为那是在循环之外。 Did you provide all of the code?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.
但是,它可以通过这种方式完成。
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.