0

Book I am reading from suggest that I make a program using functions, whiles and if.

The purpose of this program is:

Take 4 numbers from the user, 2 will be summed, 2 will be subtracted.
There are 3 functions. One is procedure_1, it is a while, it is supposed to take numbers from the user and continue the process until sum reaches <= 100 and sub reaches a value of <= 100, it also puts the results of these procedures on a list. In case it does, start() function has an if that, in case it reaches those values, it will run procedure_2, which prints a message and also prints the results of the lists.

Heres the code, but I am getting:

print "Results of sum and subtract:... %d, %d" % (sum, rest)
                                                               ^
IndentationError: unindent does not match any outer indentation

 

sum = 0
rest = 0
results = []
results_2 = []

def procedure_1():
    while sum <= 100 and rest <= 100:
        sum = n1 + n2; rest = ns1 - ns2
        print "What numbers do you wish to sum and subtract?"
            n1 = raw_input("Sum Num1:...") 
            n2 = raw_input ("Sum num2:...")
            ns1 = raw_input("Sub Num 1:...")
            ns2 = raw_input ("Sub Num 2:...")
        print "Results of sum and subtract:... %d, %d" % (sum, rest)
        results.append(sum); results_2.append(rest)
        
        sum += sum; rest += rest
        
def procedure_2():
    print "Values are too high to compute your stuff"
    for sum in results:
        print sum
    for rest in results_2:
        print rest

        
def start():
    if sum < 100 and rest < 100:
        procedure_1()
    else:
        procedure_2()

Checked and double checked it, still cant run it and see whats wrong with it, would appreciate advice on how to make this code work too. Thanks a lot.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • What are those semicolons doing in there??? – dursk Dec 23 '14 at 17:47
  • Just doing what the book told me, but if its bad form I better avoid it. – Little Foot Dec 23 '14 at 18:13
  • 1
    Either you have several transcription errors or you're using a really really bad book. – Tim Tisdall Dec 23 '14 at 19:01
  • ''Learning Python the Hard way'' is not a bad book. What do you mean by transcription errors. – Little Foot Dec 23 '14 at 19:06
  • The semi-colons, while not commonly used in python, are just fine. – Nathan Davis Dec 23 '14 at 21:30
  • On the contrary, LPTHW [is really bad](https://duckduckgo.com/?q=why+not+lpthw). – Karl Knechtel Jul 14 '23 at 19:35
  • This question was inappropriately edited after the main question was answered, in such a way that the underlying problem was no longer apparent. Please keep in mind that Stack Overflow is **not a discussion forum**, and questions here **are not** for the purpose of getting OP's code to work and **not** to discuss the code back and forth with answerers. I rolled back the change so that the question reflects the original problem being asked about, and questions address that problem. That is what questions are supposed to look like here, to avoid confusing later onlookers. – Karl Knechtel Jul 14 '23 at 19:38
  • However, this is also an extremely common problem asked about constantly, with a canonical duplicate - so I've also linked it there. – Karl Knechtel Jul 14 '23 at 19:38

1 Answers1

2

You need to make the indent of these lines match the rest of the body of the while loop:

        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

So that it looks like this:

def procedure_1():
    while sum <= 100 and rest <= 100:
        sum = n1 + n2; rest = ns1 - ns2
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")
        print "Results of sum and subtract:... %d, %d" % (sum, rest)
        results.append(sum); results_2.append(rest)

        sum += sum; rest += rest

However, you probably also want to move your calculation of the sum and so on after you ask for input:

def procedure_1():
    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        # Note: It's generally considered bad form to use semicolons in Python.
        sum = n1 + n2
        rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (sum, rest)
        results.append(sum); results_2.append(rest)

        sum += sum
        rest += rest

Finally, you don't want two different variables with the same name, so your internal variables need to have a different name to not overshadow the global ones:

def procedure_1():
    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        inner_sum = n1 + n2
        inner_rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (inner_sum, inner_rest)
        results.append(inner_sum); results_2.append(inner_rest)

        sum += inner_sum
        rest += inner_rest

Finally, because sum and rest are global variables that you're wanting to modify from inside a function, you need to note that you want to write to the global version:

def procedure_1():
    # This says to write to the global variables rather than creating local ones.
    global sum, rest

    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        inner_sum = n1 + n2
        inner_rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (inner_sum, inner_rest)
        results.append(inner_sum); results_2.append(inner_rest)

        sum += inner_sum
        rest += inner_rest

(Also note that sum is a function in Python, so you may desire to name your variable something else.)

Amber
  • 507,862
  • 82
  • 626
  • 550