Please I need help with the following exercise:

1.Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100. Write another program that reads 100 numbers from the user and prints out the sum

2. Write a function that implements Euclid's method for finding a common factor of two numbers. It works like this:

  1. You have two numbers, a and b, where a is larger than b
  2. You repeat the following until b become zero
  3. a is changed to the value of b
  4. b is changed to the remainder when a (before the change) is divided by b (before the change)
  5. you then return the last vaule of a

Hints:

  1. Use a and b as parameters to the function
  2. Simply assume that a is greater than b
  3. The remainder when x is divided by z is calculated by the expression x % z
  4. Two variables can be assigned to simultaneously like this: x, y = y, y+1. Here x is given the value of y (that is, the value y had before the assignment) and y is incremented by one.

Have you done work on this? If so, post it here. Otherwise we are not here to do your work for you.

Have you done work on this? If so, post it here. Otherwise we are not here to do your work for you.

Yes, I have worked on Exercise 1, and here is what I come up with:

onetohundred = range (1,101)
for sum in onetohundred:
print sum

after running the program, I have this screenshot:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 1.2
>>> ================================ RESTART ================================
>>>
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
>>>

NOTE: BUT I AM STILL TRYING TO FIND THE SUM FOR THE ABOVE.

the other part of Exercise 1, I have this for the solution:

list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 10]
sum = 0
for num in list:
sum = sum + num
print "The sum is", sum


after running the program, I have the screenshot:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 1.2
>>> ================================ RESTART ================================
>>>
The sum is 100
>>>


I THINK I GOT THIS ONE CORRECTLY.

I have not been able to figure out the 2nd exercise yet, still working on it though.

Thanks.

Please use the [code=python] and [/code] tag pair to enclose your python code.

Good grief, you are almost there! You have ...

mylist = [2, 4, 6, 8, 10, 12, 14, 16, 18, 10]
sum = 0
for num in mylist:
    sum = sum + num
 
print "The sum is", sum

... all you need to do is to replace the first line with ...

mylist = range(1, 101)

Oh, by the way, don't use a Python function name like 'list' for a variable name, it will screw up your program!

Hmm... It's possible that exercise 1 might be looking for something different. Notice that in your second program, you pre-define the numbers in list. But it seems like the directions want you to solicit the user for the numbers ... am I reading it right?

I fact, that seems to be true for both parts of #1.

Well, in any event, let's say that you do get your numbers, from the user or from the list. The problem is to 'accumulate' them in a sum. Here's the standard approach:

total = 0
while WhateverConditionIsTrue:
    number = GetNumberHoweverYouWant()
    total = total + number

In the coding world, the variable 'total' is called an accumulator.

Note that WhateverConditionIsTrue and GetNumberHoweverYouWant are placeholders for your own condition and number-getting method, respectively, so you'll need to provide those.

The last line, total = total + number, is often abbreviated

total += number

I hope that gets you started.

Jeff

In #2, it seems like you've been given enough hints so that if you follow up on them, you'll get it.

If you don't like hint #2 ("Assume that a > b"), which wouldn't always be true in the real world, you can force it to be true like this:

a,b = max(a,b),min(a,b)

It works like this: max(a,b) returns the larger of the two; min(a,b) returns the smaller of the two. Then a gets assigned to the max, and b gets assigned to the min.

Hmm... It's possible that exercise 1 might be looking for something different. Notice that in your second program, you pre-define the numbers in list. But it seems like the directions want you to solicit the user for the numbers ... am I reading it right?

I fact, that seems to be true for both parts of #1.

It looks like that is true, but then I thought this was a clever way to simulate a tedious 100 number entry. The summing is easy, the user input loop will be more interesting!

Thank you all for your help and contributions. You have been a great help.

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.