python语言程序设计_梁勇—第四章练习题部分答案

1、解一元二次方程 例如a*x^2 + b * x + c = 0 的平方根

import math
import random

def calculate_square_root():
    a,b,c = eval(input("Enter a , b , c:"))
    discriminant= b**2 - 4 * a * c
    if discriminant > 0:
        r1 = (-b + math.sqrt(b**2 - 4*a*c))/2 * a
        r2 = (-b - math.sqrt(b**2 - 4*a*c))/2 * a
        print("The roots are %.2f"%r1,"and%.2f"%r2)

    elif discriminant==0:
        r1 = r2 = (-b + math.sqrt(b**2 - 4 * a * c))/2 * a
        print("The roots are %.2f"%r1)
    else:
        print("The equation has no real roots!")

2、克莱姆法则解2*2方程

def Cramer_rule():

    a,b,c,d,e,f=eval(input("请输入6个相关系数: "))

    x=(e*d-b*f)/(a*d-b*c)
    y=(a*f-e*c)/(a*d-b*c)

    if a*b-b*c!=0:
        print("二元一次方程组的解为x= : ",x)
        print("二元一次方程组的解为y= : ",y)

    else:
        print("无解")

3、(游戏:学习加法)编写程序产生两个100以下的整数,然后提示用户输入这两个整数的和。

def practice_add():
    number1 = random.randint(0,100)
    number2 = random.randint(0,100)
    answer = eval(input(str(number1)+'+'+str(number2)+'=?'))
    print(number1,"+",number2,"=",answer,"is",number1+number2==answer)

4、(找未来数据)编写程序提示用户输入表示今天是一周内哪一天的数字(周天是0,周一是一,以此类推)。还要提示用户输入今天之后到未来某天的天数然后显示未来某天是星期几

def find_future_date():
    week_day = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
    today = int(input("Enter today's day:"))
    future_day = int(input("Enter the number of days elapsed since today: "))
    weekday = today % 7
    future_weekday = (today + future_day)%7
    print("Today is ",week_day[weekday],"and the future day is ",week_day[future_day])

5、(对三个整数排序)编写一个程序提示用户输入三个整数,然后以升序的方式显示它们

def arrange_num():
    a,b,c= eval(input("请输入三个整数:"))
    list1 = [a,b,c]
    list1.sort()## 直接对列表进行操作,无需使用列表=
    for i in list1:
        print(i)

6、(找出一个月中的天数)编写程序提示用户输入年和月,然后显示这个月的天数。

def find_month_days():
    month_days = ["31",'29',"31","30","31","30","31","31","30","30","30","31"]
    year,month = eval(input("Enter the year and month:"))
    if year % 400 == 0 and month == 2:
        print("%d 年 %d 月有 28 天"%(year,month))
    elif year % 400 != 0:
        print("%d 年 %d 月 有 %s 天"%(year,month,month_days[month-1]))
    # 此处隐藏知识点:elif 和 if 循环语句的差别,if与if条件是对立存在的,即if后的条件都会进行判断显示,而if与elif则是if判断成立后,不会继续判断elif后的条件。

7、(游戏:剪刀、石头、布)编写程序来玩流行的剪刀-石头-布的游戏。

def Rock_paper_scissors():
    bunch = input("石头,剪刀,布:")
    bunch_library = ['石头','剪刀','布']
    computer = bunch_library[random.randint(0,2)]
    # randint函数是包含 2 的
    # 玩家角度
    if ((bunch == '石头')and (computer == '剪刀'))or((bunch == '剪刀')and(computer == '布')) or((bunch == '布')and(computer == "石头")):
            print("电脑是%s"%computer,"你赢了")
    elif bunch == computer:
            print("电脑是%s"%computer,"平局")
    else:
        print("电脑是%s"%computer,"你输了")

8、(科学问题:一周的星期几) 泽勒的一致性是一个由泽勒开发的算法,用于计算一周的星期几。

def calculate_the_week():
    week_day = ['Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday']
    year = eval(input("Enter year :(e.g.,2008):"))
    m = eval(input("Enter month:1-12:"))
    if m == 1 or m == 2:
        m += 12
        year -= 1
    q = eval(input("Enter the day of month:1-31:"))
    k = year % 100
    j = (year / 100)//1
    h = ((q + (26*(m + 1)/10)//1) + k + ((k/4)//1) + ((j/ 4) //1+5 *j)) % 7
    print("Day of the week is %s"%week_day[int(h)])

9、(回文数)编写程序提示用户输入一个三位数,然后判断其是否为回文数

def judge_palindromic_number():
    number = eval(input("Enter a three_digit integer:"))
    a = number // 100
    c = number % 10
    if a == c:
        print("%d is a palindrome"%number)
    else:
        print("%d is not a palindrome"%number)

10、各类进制的数字转换

def tansfer_digit():
    digit = eval(input("输入十进制数:"))
    convertion = eval(input("输入要转换的进制(2、8、16)"))
    if convertion == 2:
        num = bin(digit)
    elif convertion == 8:
        num = oct(digit)
    elif convertion == 16:
        num = hex(digit)
    print(num)


if __name__ == '__main__':
    tansfer_digit()

且行且珍惜

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