2012-07-16 54 views
-1

请记住,我对python编码还是很新的,因为我只是进入python编码类的第5章。牢记这一点,我试图创建一个使用“while循环”的总和计算器来继续,直到用户输入负数而不是正数。需要帮助修复/提炼python中的while循环求和计算器

如果我不是我的,我的问题的描述完全清楚,我将发布确切的家庭作业问题就在这里:

第5章,200页,#8 数之和

写一个程序一个while循环,要求用户输入一系列正数。用户应输入一个负数来表示该系列的结束。所有正数输入后,程序应显示其总和。

现在对于我至今写的代码:

def main(): 
    number = float(input('Please enter in a positive number: ')) 
    while number > 0: 
     positiveNumber() 
    while number < 0: 
     calculateTotal() 
     printTotal() 

def positiveNumber(): 
    number = float(input('If you are finished please enter a negative number.' + \ 'Otherwise, enter another positive number: ')) 
    while number > 0: 
     positiveNumber() 
    while number < 0: 
     calculateTotal() 
     printTotal() 

def calculateTotal(): 
    total = 0 + number 

def printTotal(): 
    print('The sum of your numbers is: ', total) 

main() 
  • 在第11行,我有“+ \”标志那里,因为我想做的,以便有没有进入空间看起来更干净的文字,但这似乎并不奏效。

我很抱歉,如果这个问题似乎“不好”,但我需要帮助,使清洁/工作总和计算器。如果有人可以看一下这段代码并希望帮助我改进,我将不胜感激。谢谢!

最终编辑:

谢谢大家提供的信息丰富的答案!我学到了很多(对于“新手”=])。我用我的计算器Talon876的答案。再次感谢大家!

+1

那么......哪部分坏了? – 2012-07-16 18:11:44

+0

nooby,它真的是“新手”:-) – Levon 2012-07-16 18:13:54

+0

输入是一个坏主意......而是使用raw_input – 2012-07-16 18:14:41

回答

3

如果你想多条线路上要打印一个字符串,字符串中放了\n。 例如,

print "This is on the first line\nThis is on the second line" 

将输出

This is on the first line 
This is on the second line 

它看起来像你混合使用递归while循环(调用来自自身内部的方法)。我建议使用一个while循环和一个输入变量来检查断状态(输入为< 0)

这将是这个样子:

sum = 0 
number = float(input('Please enter in a positive number: ')) 
while number > 0: 
    sum = sum + number 
    number = float(input('If you are finished please enter a negative number.' + \ 'Otherwise, enter another positive number: ')) #fix this line using the information from the first part of the answer 

这将循环播放,直到用户输入一个负数,或0.如果要接受0作为正数,请将while条件更改为number > -1

1

如果不将其显式声明为全局变量,则无法更新python函数中的全局变量。观察到:

a = 1 
def foo(): 
    a = a + 6 #creates a new variable (a) that is confined to the "foo" namespace. 
      #Note that it still uses a from the global namespace on the Right hand side 
      #This is because python looks for a in the "foo" namespace first. When 
      #it isn't found there, it looks in the global namespace. However, python 
      #WON'T ASSIGN to something in the global namespace without being told 
      #to explicitly 
    print (a) 

foo() # 7 
print (a) # 1 

def foo(): 
    global a #Tell python that it is OK to assign to variable "a" in the global namespace. 
    a = a + 6 
    print (a) 

foo() # 7 
print (a) # 7 

但是,拥有这个巨大的力量有很大的责任。很多人会告诉你从不使用全局变量。在很多方面,它们是正确的,因为几乎所有可以用全局变量完成的任何事情都可以使用其他方法更加干净地完成。我写这篇文章的目的不是为了说服你使用全局变量,而是为了帮助你理解代码中的一个错误。

你可能想要尝试的一件事是让你的函数接受输入数字作为参数,并且总和到这一点,然后return新的总数。

祝你好运!

0

问题1.you还没有宣布你正在使用的功能作为全球性的,要注意变化的变量他们正在取得

2.you不需要while循环,如果你是通过调用实现它!递归函数您需要检查条件LIK“如果&其他” 这里是一个容易实现的问题,while循环:

def main(): 
total=0 
number = float(input('Please enter in a positive number: ')) 
while(number>0): 
    total=total+number 
    number = float(input('Please enter in a positive number to continue or a negative no. to stop: ')) 
print('The sum of your numbers is: %d'% total) 
main() 
+1

你不会在这里递归调用任何东西。 – mgilson 2012-07-16 18:44:35

0

我认为你在寻找这样的事情?但是我不知道你需要使用哪种风格约束。

number = float(input('Please enter in a positive number: ')) 
to_sum = [] 
while number > 0: 
    to_sum.append(number) 
    number = float(input('If you are finished please enter a negative number.\n' + 
         'Otherwise, enter another positive number: ')) 
print('The sume of your numbers is: ', sum(to_sum)) 

请注意,因为您试图分成多行的语句已经在()的范围内,所以您不需要。你可以打破这条线。

该作业是否需要您使用如此多的疯狂功能?另外,你使用的是哪个版本的Python?

0

你需要学习的一件事是如何将程序分解为函数。一段代码比一段代码更好地处理一些问题,我认为这是其中之一。

您需要计算一笔总和。您可以使用单个变量来处理该变量,并在用户输入时向其添加更多数字。你的代码应该围绕这个变量进行设计。如果你试图将代码分解成函数,你需要使用一个全局变量(不推荐!),或者你需要在函数中传递变量,或者你可以把变量放入一个对象中,然后该功能是对象上的“方法”功能。但最简单的方法就是编写一些使用变量的代码,并将所有代码作为一个代码块(一个函数,或者甚至只是Python程序中的代码)。

这里是一个解决方案:

sum = 0.0 # initial value; we will add values to this 
print('Welcome to this program') 
while True: 
    s = input('User: enter data value or a negative number to stop') 
    x = float(s) 
    if x < 0: 
     break 
    sum += x # add this value to update the sum 
print('Here is your sum: {}'.format(sum)) 

所以,这里是什么好对上面的代码。所有需要使用变量sum的地方都紧密结合在一起,我们可以看到它们。我们不需要声明sum全局,因为我们没有多个函数试图使用它。

看看这段代码,问自己:如果我们把它分成多个函数,它会更简单还是更清晰?如果不是,那就不要这样做。

这里唯一棘手的问题是我们使用while True:作为循环。这是因为我们想要做一些事情(得到输入),然后根据结果决定是跳出循环还是继续,然后根据决策做出其他事情(更新总和)。

这有可能改写这个使用“标志”变量,使循环while flag:,但我不认为它是清洁:

sum = 0.0 # initial value; we will add values to this 
print('Welcome to this program') 
continue_loop = True 
while continue_loop: 
    s = input('User: enter data value or a negative number to stop') 
    x = float(s) 
    if x < 0: 
     continue_loop = False 
    else: 
     sum += x # add this value to update the sum 
print('Here is your sum: {}'.format(sum)) 

你是否认为这是更清晰的有continue_loop标志变量?一些教科书说你应该这样写你的代码,因为他们认为使用break退出中间循环是一种罪过;他们认为环路应该只从通常的地方退出(对于while环路来说,它是最高的)。

如果你真的想使用函数呢?那么,你可以,但你仍然不应该使用全局变量。事实上,如果您正在编写“功能性”解决方案,则根本不需要sum变量!

这是一个功能性解决方案。

def ask_and_sum(): 
    s = input('Hey dude enter a value or a negative to stop') 
    x = float(s) 
    if x < 0: 
     return 0 
    else: 
     return x + ask_and_sum() 

print('Welcome to this program') 
print('Your sum is: {}'.format(ask_and_sum())) 

而不是显式循环,它使用“尾递归”,其中一个函数结束与另一个调用自己。在这种情况下,我个人更喜欢显式循环。你怎么看?

P.S.这个问题非常简单,如果没有给出完整的答案就很难讨论它。我为此道歉。但即使您只是复制代码,请仔细阅读并仔细考虑,并确保您了解它。