python作业2

1.Using while loop, calculate there are how many consecutive zeros from the right side of a positive integer
number.
e.g. for 123000, answer is 3
for 123456, answer is 0
for 100230, answer is 1

# Using while loop, calculate there are how many
# consecutive zeros from the right side of a positive integer number.
while True:
    a = int(input("PLEASE ENTER A POSITIVE INTEGER:"))
    c = a
    i = 0
    b = 0
    if a > 0:
        while b == 0:
            b = a % 10
            a = a // 10
            if b == 0:
                i += 1
            else:
                break
        print("FOR {} , ANSWER IS {}".format(c, i))
    else:
        print("IT IS NOT A POSITIVE INTEGER")

3.Using while loop, reverse a positive integer.
e.g. 123 -> 321
110 -> 11
5332234 -> 4322335

# Using while loop, reverse a positive integer.
while True:
    sum = "0"
    a = int(input("PLEASE ENTER A POSITIVE INTEGER:"))
    if a > 0:
        while a > 0:
            c = a % 10
            a = a // 10
            sum = str(sum) + str(c)
        print(int(sum), end="")
    else:
        print("IT IS NOT A POSITIVE INTEGER")
    print("")

4.Given an integer N, calculate the result of the expression:
1 + 2 + 3 + … + N

# Given an integer N, calculate the result of the expression:1 + 2 + 3 + … + N
while True:
    a = int(input("please enter an integer number:"))
    sum = 0
    while a > 0:
        sum = sum + a
        a -= 1
    print("the sum is {}".format(sum))

尾递归

def factorial_tail_recursion(n, res = 0):
    if n == 0:
        return res
    return factorial_tail_recursion(n-1, res + n)

print(factorial_tail_recursion(4))

5.Given an integer N, calculate the result of the expression
1 * 2 * 3 * … * N
求阶乘和

使用递归:

#factorial in recursion
a = int(input("please enter an integer:"))
c = a
def func_factorial( a ):
    if (a <= 1):
        return 1
    return func_factorial( a-1 ) * a
func_factorial( a )
print("the factorial of {} is {}".format(c, func_factorial( a )))

尾递归

def factorial_tail_recursion(n, res = 1):
    if n == 0:
        return res
    return factorial_tail_recursion(n-1, res * n)

朴实无华:

# Given an integer N, calculate the result of the expression:1 * 2 * 3 * … * N
while True:
    a = int(input("please enter an integer number:"))
    fac = 1
    c = a
    while a > 0:
        fac = fac * a
        a -= 1
    print("the factorial of {} is {}".format(c, fac))

6.Using while loop to generate(print) all of even numbers
ranging from 0 to 1000(1000 is included)

# Using while loop to generate(print) all of even numbers
# ranging from 0 to 1000(1000 is included)
i = 0
j = 0
while i <= 1000:
    print(i,end=' ')
    i += 2
    j += 1
    if j == 30:     # every 30 numbers create a new line
        print("")
        j = 0
print("end")

6.Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included)

# Write a Python program to find those numbers which are
# divisible by 7 and multiple of 5, between 1500 and 2700 (both included)
for x in range(1500, 2701):
    a = x % 7
    b = x % 5
    if a == 0 and b == 0:
        print(x, end=" ")

7. Write a Python program to construct the following pattern,
using a nested for loop.

·
· ·
· · ·
· · · ·
· · · · ·
· · · ·
· · ·
· ·
·

for i in range(1, 6):
    for j in range(i):
        print("*", end = ' ')
    print("")
for i in range(1, 5):
    for j in range(5 - i):
        print("*", end = ' ')
    print("")

8. Write a Python program to count the number of even and odd numbers from a series of numbers.

# Write a Python program to count the number of even and odd
# numbers from a series of numbers.
import ast
while True:
    list = ast.literal_eval(input("please enter a serial of numbers:"))
    even = 0
    odd = 0
    for i in list:
        if i % 2 != 0:
            odd += 1
        else:
            even += 1
        print(i)
    print("Number of even numbers:{}".format(even))
    print("Number of odd numbers:{}".format(odd))

9. Write a Python program which iterates the integers from50. For multiples of three print “Fizz” instead of the numberfor the multiples of five print “Buzz”. For numbers which aremultiples of both three and five print “FizzBuzz".

# Write a Python program which iterates the integers from 1 to
# 50. For multiples of three print "Fizz" instead of the number and
# for the multiples of five print "Buzz". For numbers which are
# multiples of both three and five print “FizzBuzz".
for x in range(1, 51):
    if x % 3 == 0 and x % 5 == 0:
        print("fizzbuzz")
    elif x % 3 == 0:
        print("fizz")
    elif x % 5 == 0:
        print("buzz")
    else:
        print(x)

你可能感兴趣的:(py作业,python)