Python_HomeWork_Day02_2

Python_HomeWork_Day02_2
Python_HomeWork_Day02_2_第1张图片
答案:

import math

print("输入a:")
a = float(input(">>>"))
print("输入b:")
b = float(input(">>>"))
print("输入c:")
c = float(input(">>>"))

s = b ** 2 - 4 * a * c
print("判别式的结果为:%f"%s)

if s>0:
    print("方程有两个实根")
elif s == 0:
    print("方程有一个实根")
elif s <0:
    print("方程没有实根")

r1 =((-b) + math.sqrt(b ** 2 - 4 * a * c))/2 *a
r2 =((-b) - math.sqrt(b ** 2 - 4 * a * c))/2 *a

print("r1= %f"%r1)
print("r1= %f"%r2)


2.答案:

import numpy as np

num1 = np.random.randint(10,100)
num2 = np.random.randint(10,100)

print("%d %d"%(num1,num2))

print("用户随机输入这俩这数字之和:")
number = int(input(">>>"))

if number == num1 + num2:
    print("True")
else:
    print("False")

3.答案:

print("输入0~6数字:")

day = int(input(">>>"))
number = int(input(">>>"))

if day == 0:
    print("今天是星期日")
    print("%d后是星期%d"%(number,(day+number)%7))
elif day == 1:
    print("今天是星期一")
    print("%d后是星期%d"%(number,(day+number)%7))
elif day == 2:
    print("今天是星期二")
    print("%d后是星期%d"%(number,(day+number)%7))
elif day == 3:
    print("今天是星期三")
    print("%d后是星期%d"%(number,(day+number)%7))
elif day == 4:
    print("今天是星期四")
    print("%d后是星期%d"%(number,(day+number)%7))
elif day == 5:
    print("今天是星期五")
    print("%d后是星期%d"%(number,(day+number)%7))
elif day == 6:
    print("今天是星期六")
    print("%d后是星期%d"%(number,(day+number)%7))

4.答案:

l=[]
for i in range(3):
    x=int(input("Integer:"))
    l.append(x)
l.sort()
print(l)


5.答案:

print("比较两种大米的“性价比”,分别输入大米的重量和价钱:")

a1,a2 = map(float,(input(">>>").split()))
b1,b2 = map(float,(input(">>>").split()))

A = a2 /a1
B = b2 /b1
if A > B:
    print("第一种大米合算")
elif B > A:
    print("第二种大米合算")
elif:
    print("都一样")

6.答案:


print("依次输入年份和月份")
years = int(input("年份>>>"))
month = int(input("月份>>>"))

if years % 4 ==0 and years%100 != 0 or years%400 != 0:
    print("今年是闰年")
    if month == 2:
        print("这个月有28天")
    elif month == 1 or 3 or 5 or 7 or 8 or 10 or 12:
        print("这个月有31天")
    else:
        print("这个月共30天")
else:
    print("今年是平年")
    if month == 2:
        print("这个月有29天")
    elif month == 1 or 3 or 5 or 7 or 8 or 10 or 12:
        print("这个月有31天")
    else:
        print("这个月共30天")

7.答案:

import numpy as np

print("用户猜硬币,0:正面,1:反面:")
res = np.random.randint(0,2)
print("%d"%res)
x = int(input("请输入0或者1:"))
if x == int(res):
    print("你猜对了")
else:
    print("你猜错了")

8.答案:

# 和电脑玩见到石头布
import numpy as np

print("电脑剪刀石头布")
print("1.剪刀/2.石头/3.布")

print("请输入你的选择:")
X = int(input(">>"))

# res = np.random.choise(["剪刀"],["石头"],["布"])
res = np.random.randint(1,4)
while True:
    if X ==1 and X == int(res):
        print("电脑出:%d"%res)
        print("平局")
    elif X ==1 and int(res) == 2:
        print("电脑出:%d"%res)
        print("电脑胜利")
        break
    elif X== 1 and int(res) ==3:
        print("电脑出:%d"%res)
        print("你赢了")
        break
    elif X == 2 and X == int(res):
        print("电脑出:%d"%res)
        print("平局")
    elif X == 2 and int(res) == 3:
        print("电脑出:%d"%res)
        print("电脑胜利")
        break
    elif X== 2 and int(res) == 1:
        print("电脑出:%d"%res)
        print("你赢了")
        break
    elif X == 3 and X == int(res):
        print("电脑出:%d"%res)
        print("平局")
    elif X == 3 and int(res) == 1:
        print("电脑出:%d"%res)
        print("电脑胜利")
        break
    elif X== 3 and int(res) == 2:
        print("电脑出:%d"%res)
        print("你赢了")
        break    


答案:


year = int(input("请输入年份>>>"))
month = int(input("请输入月份(1-12)>>>"))
day = int(input("请输入这个月的某一天(1-31)>>>"))

if month == 1:
    month =13
    year -= 1
    h = (day +  (26 * (month + 1)/ 10) + (year%100) +  ((year%100)/4) +  ((year/100)/4) +  5* (year/100) )%7
elif month == 2:
    month = 14
    year -= 1
    h = (day +  (26 * (month + 1)/ 10) + (year%100) +  ((year%100)/4) +  ((year/100)/4) +  5* (year/100) )%7
else:
   h = (day +  (26 * (month + 1)/ 10) + (year%100) +  ((year%100)/4) +  ((year/100)/4) +  5* (year/100) )%7
h = h//1
print(h)
if h == 2:
    print("今天是星期日")
elif h == 3:
    print("今天是星期一")
elif h == 4:
    print("今天是星期二")
elif h == 5:
    print("今天是星期三")
elif h == 6:
    print("今天是星期四")
elif h == 0:
    print("今天是星期五")
elif h == 1:
    print("今天是星期六")

Python_HomeWork_Day02_2_第2张图片
10.答案:

import numpy as np

print("扑克牌的大小分别是:1(Area)、2、3、4、5、6、7、8、9、10、11(Jack)、12(Queen)、13(King)")
print("扑克牌的花色:1:梅花、2:红桃、3:方块、4:黑桃")

number = int(input("输入你选择的牌的大小:"))

# number1 = np.random.randint(1,14)
if number == 1:
    print("你选择的是:Area")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 2:
    print("你选择的是:2")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 3:
    print("你选择的是:3")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 4:
    print("你选择的是:4")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 5:
    print("你选择的是:5")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 6:
    print("你选择的是:6")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 7:
    print("你选择的是:7")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 8:
    print("你选择的是:8")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 9:
    print("你选择的是:9")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 10:
    print("你选择的是:10")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 11:
    print("你选择的是:Jack")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 12:
    print("你选择的是:Queen")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
elif number == 13:
    print("你选择的是:King")
    number2 = np.random.randint(1,5)
    if number2 ==1:
        print("梅花")
    elif number2 ==2:
        print("红桃")
    elif number2 ==3:
        print("方块")
    else:
        print("黑桃")
else:
    print("请重新输入!!!")

11.答案:

number = float(input("判断是否是回文数,请输入一个三位整数:"))

number1 = number//100 #百位
number2 = number%10   #个位
number3 = (number - 100)//10 # 十位

if number1 == number2:
    print("%d是一个回文数")
else:
    print("%d不是回文数")

12.答案:

a = float(input("输入三角形的a边长:"))
b = float(input("输入三角形的b边长:"))
c = float(input("输入三角形的c边长:"))

if a+b>c and a+c>b and b+c>a:
    print("能够成三角形")
    print("三角形的周长是:%d"%(a+b+c))
else:
    print("不能够成三角形")

你可能感兴趣的:(算法练习题,Python)