Reputation: 97
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.
Upvotes: 0
Views: 217
Reputation: 527558
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.)
Upvotes: 2