《python语言程序设计》(2018版)第6章编程题 第25题~第32题代码

在这里插入图片描述

def isPrime(number):
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
            return False
        divisor += 1
    return True


def inverse_prime(number):
    if number <= 99:
        b = number % 100 // 10
        c = number % 100 % 10
        return f"{c}{b}  "

    elif number >= 100:
        a = number // 100
        b = number % 100 // 10
        c = number % 100 % 10
        return f"{c}{b}{a}"


def print_prime_numbers(number_of_primes):
    number_of_primes_per_line = 10
    count = 0
    number = 2
    while count < number_of_primes:
        if isPrime(number):
            count += 1
            a = inverse_prime(number)
            print(format(a, ">4"), end=" ")
            if count % number_of_primes_per_line == 0:
                print()

        number += 1


def main():
    print("The first 100 prime number are")
    print_prime_numbers(100)


main()

在这里插入图片描述


def isPrime(number):
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
            return False
        divisor += 1
    return True


def mersenne_prime(number):
    a = 2 ** number - 1
    return format(a, "<10d")


def print_number(number):
    number_of_primes_per_line = 31
    count = 0
    while count < number_of_primes_per_line:
        count += 1
        if isPrime(number):
            print(f"This %2d is mersenne prime," % number, mersenne_prime(number))
        number += 1


def main():
    print_number(2)


main()

这个代码的编写思想,我承认很无耻。

在这里插入图片描述



def isPrime(number):
    divisor = 2
    while divisor <= number / 2:
        if number % divisor == 0:
            return False
        divisor += 1
    return True


def contrast(number):
    a = number + 2
    print(a)


def printPrimeNumbers(number_of_primes):
    count = 0
    number = 3
    while count < number_of_primes:
        if isPrime(number):
            print(number, end=" ")
            contrast(number)
        count += 1
        number += 1


def main():
    print("The less than 1000 prime number are")
    printPrimeNumbers(999)


main()

在这里插入图片描述

from random import *

 def plus_method(num1, num2):
     a = num1 + num2
     return a


 def main():
     while True:
         num1 = randint(0, 6)
         num2 = randint(0, 6)
         a = plus_method(num1, num2)
         if a == 2 or a == 3 or a == 12:
             print(f"{num1} + {num2} = {a}")
             print("You lose")
             break
         elif a == 7 or a == 11:
             print(f"{num1} + {num2} = {a}")
             print("You Win")
             break
         else:
             print(f"{num1} + {num2} = {a}")
             print(f"Point is {a}")
             continue

 main()

在这里插入图片描述


def n_odd(number):
    a = number // 1000
    a_m = a * 2
    c = number % 1000 // 10 % 10
    c_m = c * 2
    if a_m >= 10 and c_m <= 9:
        odd_a_1 = a_m // 10
        odd_a_2 = a_m % 10
        odd_a_sum = odd_a_1 + odd_a_2
        return odd_a_sum + c_m
    elif c_m >= 10 and a_m <= 9:
        odd_c_1 = c_m // 10
        odd_c_2 = c_m % 10
        odd_c_sum = odd_c_1 + odd_c_2
        return odd_c_sum + a_m

    elif a_m >= 10 and c_m >= 10:
        odd_a_1 = a_m // 10
        odd_a_2 = a_m % 10
        odd_a_sum = odd_a_1 + odd_a_2
        odd_c_1 = c_m // 10
        odd_c_2 = c_m % 10
        odd_c_sum = odd_c_1 + odd_c_2
        return odd_c_sum + odd_a_sum
    elif a_m <= 9 and c_m <= 9:
        return a_m + c_m


def n_even(number):
    b = number % 1000 // 100
    d = number % 10
    return b + d


def isValid(n1, n2, n3, n4, a, b):
    c = a + b
    d = c % 10
    if d == 0:
        print(f"This {str(n1)} {str(n2)} {str(n3)} {str(n4)} credit card is valid")
        print(d)
        print(c)
    else:
        print(f"This {str(n1)} {str(n2)} {str(n3)} {str(n4)} credit card is invalid")
        print(d)
        print(c)


def main():
    # number1 = eval(input("Input credit card number: "))
    # number2 = eval(input("Input credit card number: "))
    # number3 = eval(input("Input credit card number: "))
    # number4 = eval(input("Input credit card number: "))
    number1 = 4388
    number2 = 5760
    number3 = 1840
    number4 = 2626  # 2001是valid
    a = n_odd(number1) + n_odd(number2) + n_odd(number3) + n_odd(number4)
    b = n_even(number1) + n_even(number2) + n_even(number3) + n_even(number4)

    isValid(number1, number2, number3, number4, a, b)


main()

在这里插入图片描述


from random import *


def plus_method(num1, num2):
    a = num1 + num2
    return a


def main():
    count = 1
    you_win = 1
    you_lose = 1
    with_draw = 1
    while count <= 10000:
        count += 1
        num1 = randint(0, 6)
        num2 = randint(0, 6)
        a = plus_method(num1, num2)
        if a == 2 or a == 3 or a == 12:
            print(f"{num1} + {num2} = {a}")
            print("You lose")
            you_lose += 1
        elif a == 7 or a == 11:
            print(f"{num1} + {num2} = {a}")
            print("You Win")
            you_win += 1
        else:
            print(f"{num1} + {num2} = {a}")
            print(f"Point is {a}")
            with_draw += 1

    print(f"You win is {you_win} and you lose is {you_lose} with draw {with_draw}")


main()

在这里插入图片描述

import time

def current_time():
    cu_time = time.time()
    return cu_time


def total_seconds():
    to_seconds = int(current_time())
    return to_seconds


def current_seconds():
    cu_seconds = total_seconds() % 60
    return cu_seconds


def total_minutes():
    to_minutes = total_seconds() // 60
    return to_minutes


def current_minutes():
    cu_minutes = total_minutes() % 60
    return cu_minutes


def current_hour():
    total_hours = total_minutes() // 60
    cu_hour = total_hours % 24
    return cu_hour + 8


def total_day():  # compute to since 1970.01.01 second
    total_hours = total_minutes() // 60 // 24
    return total_hours


def run_day_in_year():  # This is the best part
    year = 1970
    a = total_day()
    while a >= 366:
        if is_leap_year(year):
            a -= 366
        else:
            a -= 365

        year += 1
    return a


#
def current_year():  # I don't know how to call the two results separately,right
    year = 1970
    a = total_day()
    while a >= 366:
        if is_leap_year(year):
            a -= 366
        else:
            a -= 365

        year += 1
    return year


def get_month_name(month):
    if month == 1:
        month_name = "January"
    elif month == 2:
        month_name = "February"
    elif month == 3:
        month_name = "March"
    elif month == 4:
        month_name = "April"
    elif month == 5:
        month_name = "May"
    elif month == 6:
        month_name = "June"
    elif month == 7:
        month_name = "July"
    elif month == 8:
        month_name = "August"
    elif month == 9:
        month_name = "September"
    elif month == 10:
        month_name = "October"
    elif month == 11:
        month_name = "November"
    else:
        month_name = "December"
    return month_name


def is_leap_year(year):
    return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)


def current_day():
    a = run_day_in_year()
    month_day = 1
    month_text = month_day
    while a >= 28:
        month_day += 1
        if month_day == 1 or month_day == 3 or month_day == 5 or month_day == 7 or month_day == 8 or \
                month_day == 10 or month_day == 12:
            a -= 31
        if month_day == 4 or month_day == 6 or month_day == 9 or month_day == 11:
            a -= 30
        if month_day == 2:
            if is_leap_year(current_year()):
                a -= 29
            else:
                a -= 28

    print(f"current day is {a + 1} and {get_month_name(month_day)}")


def main():
    print("This year is", current_year())
    current_day()
    print("Current time is", current_hour(), ":",
          current_minutes(), ":", current_seconds(), "GMT")


main()

在这里插入图片描述


def month_list(month_data):
    month_dist = {1: "一月", 2: "二月", 3: "三月", 4: "四月", 5: "五月", 6: "六月", 7: "七月", 8: "八月", 9: "九月",
                  10: "十月", 11: "十一月", 12: "十二月"}
    return month_dist[month_data]


def week_list(w_day):
    week_dist = {0: "星期六", 1: "星期日", 2: "星期一", 3: "星期二", 4: "星期三", 5: "星期四", 6: "星期五"}
    return week_dist[w_day]


def count_day(year_data, month_num, input_data):
    year_data_type = year_data
    after_years = year_data - 1

    for i in range(month_num, 13):
        month_day = i
        if i <= 2:
            i += 12
            year_data = after_years

        elif i >= 3:
            i = i
            year_data = year_data_type
            # print(i,year_data)

        j = year_data // 100
        k = year_data % 100
        m1 = (26 * (i + 1)) // 10
        w_day = (input_data + m1 + k + (k // 4) + (j // 4) + (5 * j)) % 7
        print(month_list(month_day), input_data, year_data_type, "is", week_list(w_day))


def main():
    year_data = eval(input("Enter year number is: "))
    month_num = eval(input("Enter month number is: "))
    input_data = eval(input("Enter day number is: "))
    count_day(year_data, month_num, input_data)


main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电饭叔

谢谢各位兄弟们的关注,谢谢破费

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值