Loop
Exercise 1: within 100 seeking all odd sum
result = 0 # Create a variable outside
in range (100) for i: # range ( print 0-100)
IF (2% I = 0!): # Use % to determine whether the odd
SUM = I +
Print (Result)
or:
I = 0
Result = 0
the while I <100:
I + =. 1
iF (2% I = 0!):
Result = I +
Print (Result)
Exercise 2: within 100 seeking all multiples and 7, and the number of
result = 0 # 7 and all
number = 0 # Counters
for I in Range (100):
IF (I == 0 7%):
Result = + I
number + =. 1
Print (Result, "sum")
Print (number, "total quantity")
Exercise 3: refers to a number of n bits daffodils (n> = 3), on which digital bits of each screen equals n times itself (for example: (1 ** ** 3 + 3 + 5 3 * 3 = 153))
find all the numbers less than 1000 daffodils
acquired within the three-digit 1000
for I in Range (100,1000):
# = hundreds digit a, b = ten digits, c = bits number
# hundreds digit
A = (i // 100)
# tens digit
B = (10% i // 10)
# digits
C = (i 10%)
# determines whether or not i is narcissistic number
if (a * **. 3. 3 + B * + C == I **. 3):
Print (I)
Practice 4: acquiring arbitrary number input by the user, determines whether it is a prime number. It refers to the prime natural numbers greater than 1, and in addition 1 itself is no longer a natural number of other factors
num = input ( "Enter a integer greater than 1:")
flage = True
# isdigit () method for detecting whether a string consisting only of numbers, if string contains only numeric return True else return False.
# Num determines whether a valid number, is greater than an
IF num.isdigit () == False:
Print ( "Please enter an integer greater than 1")
elif 1> = int (num):
Print ( "Please enter a an integer greater than 1 ")
# determines yum if is a prime number, can only be 1 and he came divisible is a prime number
for I in Range (2, int (NUM)):
iF (int (NUM)% I == 0) :
# Once actually determines the proof num not a prime number, the flage modify to false
flage = False
IF flage:
Print (num, "is a prime"),
the else:
Print (num, "is not a prime number")
or with:
num = INPUT ( "Please enter an integer greater than 1:")
flage = True
IF num.isdigit () == False:
Print ( "
1 elif> = int (NUM):
Print ( "Please enter an integer greater than 1")
I 2 =
the while I <int (NUM):
IF int (NUM) == 0% I:
flage = False
I = 1 +
IF flage:
Print (NUM, "is a prime"),
the else:
Print (NUM, "is not a prime number")
nested loops:
exercise 1:
Print 99 multiplication table
I = 1
the while I <10:
J = 1
the while J <I + 1'd:
Print (J, "X", I, "=", I * J, End = "")
J + =. 1
Print ()
I =. 1 +
练习2:
求100以内的质数
falge = True
for i in range(2, 101):
falge = True
for j in range(2, i):
if ( i % j == 0):
falge = False
if falge:
print(i, "是质数!")
或者:
i = 2
while i <= 100:
falge = True
j = 2
while j < i:
if (i % j == 0):
falge = False
j +=1
if falge:
print(i)
i += 1