[source code] Python Programming Tutorial - 10 - comments and break

+3 Bucky Roberts · August 27, 2014

magicNumber = 26

for n in range(101):
    if n is magicNumber:
        print(n, " is the magic number!")
        break
    else:
        print(n)

'''
*** Challenge 1 ***
Create a program that lists all the numbers between 1-100 that are divisible by four.
Good luck!
'''

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Murphy · September 10, 2014
for num in xrange(101):
    if num%4==0:
        print num, 'is divisible by 4'
0 Theo Jay · September 7, 2014
Programming challenge (HW):

# print numbers divisible by 4

counter = 0 # introduced to help produce a table

for num in range(1,101):
    if num % 4 is 0:
        print(num, " ", end="") # print elements on same line
        counter += 1

        if counter is 5:
            print()
            counter = 0
+2 Yoncho Yonchev · August 27, 2014
Challenge accepted!!!
 [ x for x in range(1,101) if x %4 ==0]

Solved in 1 line of code. and if you want to print the answer:

print(' '.join([str(x) for x in range(1,101) if x %4 ==0]))
+5 Aubree Keegan · August 27, 2014
I haven't seen the video, but here is my reply for the challenge. Please make more challenges, they are fun! :happy:

for x in range(101):
    if x % 4 is 0:
        print(x, "is divisible by zero!")
  • 1

Python

107,334 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator