Python 7 Levels, L3, Unit 22, For Loop

Powered By EmbedPress

Exercise 1:

for i in [1,2,3,4,5,6,7,8,9]:
    print(i,"* 9 = ",i*9)

Exercise 2:

number = int(input("Please input an integer greater than 1000:"))
sum = 0;
if number > 1000:
    for i in range(number):
        if i%7 == 0:
            sum += i
    print("sum of all the numbers can be divided by 7 less than ", number," is: ",sum)
else:
    print("Invalid input")

Exercise 2, another way:

n = int(input("Please input a integer greater than 1000")
sum = 0
for i in range(0,n,7):
    sum += i
print(sum)

Homework:

result = 0
for i in range(1000):
    if i%2 == 0:
        result -= i
    else:
        result += i
print("1-2+3-4+....+999=",result)

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts