C++ For Dummies - Chapter 5: NestedDemo rewritten in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// NestedDemo - input a series of numbers.
// Continue to accumulate the sum
// of these numbers until the user
// enters a 0. Repeat the process
// until the sum is 0.
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// the outer loop
cout << "This program sums multiple seriesn"
<< "of numbers. Terminate each sequencen"
<< "by entering a negative number.n"
<< "Terminate the series by entering twon"
<< "negative numbers in a rown";
// continue to accumulate sequences
int accumulator;
for(;;)
{
// start entering the next sequence
// of numbers
accumulator = 0;
cout << "Start the next sequencen";
// loop forever
for(;;)
{
// fetch another number
int nValue = 0;
cout << "Enter next number: ";
cin >> nValue;
// if it's negative...
if (nValue < 0)
{
// ...then exit
break;
}
// ...otherwise add the number to the
// accumulator
accumulator += nValue;
}
// exit the loop if the total accumulated is 0
if (accumulator == 0)
{
break;
}
// output the accumulated result and start over
cout << "The total for this sequence is "
<< accumulator
<< endl << endl;
}
// we're about to quit
cout << "Thank you" << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#! /usr/bin/python
print "This program sums multiple seriesnof numbers. Terminate each sequencenb
y entering a negative number.nTerminate the series by entering twonnegative nu
mbers in a row"
def sumSequence():
negative = True
sum = 0
while negative == True:
inputnumber = int(raw_input("Enter next number: "))
if inputnumber >= 0:
sum += inputnumber
else:
negative = False
return sum
def startSum():
returnZero = True
while returnZero == True:
print "Start the next sequence"
u = sumSequence()
if u > 0:
print "The total for this sequence is "+str(u)
else:
returnZero = False
print "Thank you"
startSum()
raw_input()
#Transfusion's rewriting

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×