好。这是IS作业...... [英] Ok. This IS homework ...

查看:70
本文介绍了好。这是IS作业......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是我一直在努力解决这个问题太长时间了,我即将开始敲打我的脑袋。


我的任务看起来很简单:创建一个程序,它将运行用户输入总数的

直到达到100为止。在100时它应该停止。

那不是事实上,这个问题是有效的。这是添加

不起作用。我的程序如何添加2 + 7并得出14?


我正在发布我的代码(这样你们都可以大笑)。如果任何人对我的错误有任何想法,我会很感激。


---------- -----------------------------------------


running = True

goal = 100

#subtotal = 0

#running_total = subtotal + guess

运行时


guess = int(raw_input(''输入我可以用来添加的整数:''))

小计=猜

运行时


guess = int(raw_input(''我需要另一个数字:'')) />
running_total = guess + subtotal

print running_total

如果running_total == goal:

print''恭喜!你已经完成了。''


elif running_total目标:

打印''这是一个很好的数字,但是太高了。再试一次。''


打印''完成''


--------------- -----------


我尝试添加额外的while声明捕获第二个

数字,但它似乎没有解决我的问题。帮助!

解决方案

" spawn" < ev ************* @ gmail.comwrites:


这不是问题,事实上,那部分作品。这是添加

不起作用。我的程序如何在运行时添加2 + 7并得出14?



guess = int(raw_input(''我需要另一个号码) :''))

running_total = guess + subtotal

print running_total



你永远不会修改运行总。我想你想要
将运行总数初始化为循环外的第一个猜测,然后

当你读到一个新数字时加上猜测。你可以像这样添加一个变量



running_total + =猜


这是约。同样的事情


running_total = running_total + guess


我不认为小计和running_total需要是单独的变量。 />


"菌种" < ev ************* @ gmail.comwrote:


但是我一直在努力解决这个问题太久了我打算开始在墙上撞我的头。

------------------- -------


我尝试添加额外的while声明捕获

的第二个数字,但它似乎没有解决我的问题。帮助!



重读你所拥有的关于while循环的任何材料,或者只是参考一下python文档中的
,然后想一想你的程序为什么(因为

无论如何都是打印的。)永远不会打印完毕。


希望这不是太多的帮助,

max


spawn写道:

运行时


guess = int (raw_input(''输入一个我可以用来添加的整数:''))

小计=猜
运行时


guess = int(raw_input(''我需要另一个数字:''))

running_total = guess + subtotal



您继续将原始输入(小计)添加到最近的猜测,

和打印running_total。你永远不会在最深的循环中重新分配小计或者使用

的running_total。


另外,你永远不会突破你最深的循环,为什么你无论如何使用两个

嵌套无限循环?


问候,

Brett Hoerner


but I''ve been struggling with this for far too long and I''m about to
start beating my head against the wall.

My assignment seemed simple: create a program that will cacluate the
running total of user inputs until it hits 100. At 100 it should stop.
That''s not the problem, in fact, that part works. It''s the adding
that isn''t working. How can my program add 2 + 7 and come up with 14?

I''m posting my code (so that you may all laugh). If ANYONE has any
ideas on what I''m doing wrong, I''d appreciate.

---------------------------------------------------

running = True
goal = 100

# subtotal = 0
# running_total = subtotal + guess

while running:
guess = int(raw_input(''Enter an integer that I can use to add : ''))
subtotal = guess

while running:
guess = int(raw_input(''I\''ll need another number : ''))
running_total = guess + subtotal
print running_total

if running_total == goal:
print ''Congratulations! You\''re done.''

elif running_total goal:
print ''That\''s a good number, but too high. Try again.''

print ''Done''

--------------------------

I tried adding an additional "while" statement to capture the second
number, but it didn''t seem to solve my problem. Help!

解决方案

"spawn" <ev*************@gmail.comwrites:

That''s not the problem, in fact, that part works. It''s the adding
that isn''t working. How can my program add 2 + 7 and come up with 14?
while running:
guess = int(raw_input(''I\''ll need another number : ''))
running_total = guess + subtotal
print running_total

You are never modifying the running total. I think you want to
initialize the running total to the first guess outside the loop, then
add guess to it when you read a new number. You can add to a variable
like this:
running_total += guess

this is approx. the same thing as

running_total = running_total + guess

I don''t think subtotal and running_total need to be separate variables.


"spawn" <ev*************@gmail.comwrote:

but I''ve been struggling with this for far too long and I''m about
to start beating my head against the wall.
--------------------------

I tried adding an additional "while" statement to capture the
second number, but it didn''t seem to solve my problem. Help!

Reread whatever material you have about while loops, or just consult
the python documentation, and then think about why your program(as
posted anyway) is never printing done.

Hope this isn''t too much help,
max


spawn wrote:

while running:
guess = int(raw_input(''Enter an integer that I can use to add : ''))
subtotal = guess
while running:
guess = int(raw_input(''I\''ll need another number : ''))
running_total = guess + subtotal

You keep adding the original input (subtotal) to the most recent guess,
and printing "running_total". You never re-assign subtotal or make use
of running_total in your deepest loop.

Also, you never break out of your deepest loop, why are you using two
nested infinite-loops anyway?

Regards,
Brett Hoerner


这篇关于好。这是IS作业......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆