First Question:
Use the following variables: i , lo , hi , and result . Assume that lo and hi each are associated with an int and that result refers to 0 .
Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result .
Your code should not change the values associated with lo and hi . Also, just use these variables: i ,lo , hi , and result .

This is what I have:

i = 0

while lo < hi:
	result = i
	i += 1
	lo += 1


if lo == hi: 
	result = 0

For some reason... this doesn't work.

Second Question:
An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.

Given the positive integer distance and the integers m and n , create a list consisting of the arithmetic progression between (and including) m and n with a distance of distance (if m > n, the list should be empty.) For example, if distance is 2, m is 5, and n is 12, the list would be [5, 7, 9, 11] .

Associate the list with the variable arith_prog .

What I have:

arith_prog = []
i = 0 
if m > n:
    []
while i <= n:
    arith_prog[::distance]
    arith_prog.append(i)
    i += distance

Also something wrong... thanks for the help.

Recommended Answers

All 2 Replies

You should write algorithms describing what you want to do

# first algorithm

sum starts from 0
i starts from lo
while i <= hi:
    add i to sum
    increase i

# second algorithm

create a new empty list
i starts from m
while i <= n:
    append i to the list
    increase i by d

Now here comes the python code

# first algorithm

sum = 0             # sum starts from 0
i = lo              # i starts from lo
while i <= hi:      # while i <= hi:
    sum += i        #    add i to sum
    i += 1          #    increase i

# second algorithm

arith_prog = []             # create a new empty list
i = m                       # i starts from m
while i <= n:               # while i <= n:
    arith_prog.append(i)    #     append i to the list
    i += d                  #     increase i by d

yesss thanks so much @_@

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.