计算机二级python指导用书编程题答案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangxingfan316/article/details/83932856

个人手写,欢迎指正

更新中。。。

#!/user/bin/env python
#-*- coding:utf-8 -*-
#author:M10
import random
#3-1输入整数,输出百分位及以上的数字
'''
s = input("输入整数哦")
print(s[:-2])
'''
#3-2获取输入字符串,按照空格分割,按行打印
'''
i = input("输入字符串")
s = i.split()
for j in s:
    print(j)
'''
#3-3输入数字1-7,输出星期几
"""
i = int(input('input a number between 1-7:'))
if i == 1:
    print('monday')
elif i == 2:
    print('tuesday')
elif i == 3:
    print('wednesday')
elif i == 4:
    print('thursday')#后续省略
else:
    print('sunday')
    
"""
#3-4回文数,正反排列相同的数
"""
s = input("input a 5-num")
if s[0]==s[-1] and s[1]==s[-2]:
    print('回文')
else:
    print('不是回文')
"""
#3-5输入十进制整数,分别输出二进制、八进制、十六进制字符串
"""
s = int(input('a number:'))
print('二进制:{:b}'.format(s))
print('八进制:{:o}'.format(s))
print('十六进制:{:x}'.format(s))
"""
#4-1输入一个年份,判断是否是闰年
"""
i = int(input('input a year:'))
if (i%4==0 and i%100 != 0) or i%400==0 :
    print(i,'闰年')
else:
    print('不是闰年')
"""
#4-2最大公约数与最小公倍数
"""
a,b = map(int,input('输入两个数,逗号分割:').split(','))
x = a*b
if a>b:
    while True:
        s = a%b
        if s ==0:
            print('最大公约数:',b)
            print('最小公倍数:',int(x/b))
            break
        b,a = s,b
elif a<b:
    while True:
        s = b%a
        if s ==0:
            print('最大公约数:',a)
            print('最小公倍数',int(x/a))
            break
        a,b = s,a
else:
    print('最大公约数:',a)#两数相等
    print('最小公倍数',int(x/a))
"""
#4-3统计不同字符个数
"""
z,s,k,q = 0,0,0,0
str = input('输入字符:')
l = len(str)
for i in range(l):
    if '0' <= str[i] <='9':
        z += 1
    elif 'A'<=str[i]<='z':
        s += 1
    elif str[i] == ' ':
        k +=1
    else:q+=1
print('整数',z)
print('英文字符',s)
print('空格',k)
print('其他',q)
"""
#4-4猜数有戏续
"""
ran = random.randint(1,1000)
count=0
while True:

    try:
        i = eval(input('pls input a num:'))
    except:
        print('类型错误,重新输入')
        continue
    if type(i) != type(1):
        print('类型错误,重新')
        continue
    else:
        count += 1
        if i >ran:
            print("it's bigger,{}次".format(count))
        elif i<ran:
            print("it's smaller,{}次".format(count))
        else:
            print("Right,{}次".format(count))
            break
"""
#4-5羊车门问题
#十万次随机实验,不换猜到车的次数为count_n,交换猜到车次数为count
"""
j=1
count_n = 0
count = 0
while j<=100000:
    j +=1
    i= random.randint(0,2)#随机猜一个数字,0车,12羊
    #若猜对车则不换+1,若猜对羊则换+1
    if i == 0:
        count_n +=1
    else:
        count +=1
print("don't change",count_n/100000)
print("change",count/100000)
"""

#5-1实现isNum函数
"""
def isNum(s):
    if type(s)==type(1+1j) or type(s)==type(1) or type(s)==type(1.1):
        return 1
    else:
        return 0
"""
#5-2实现isPrime函数
"""
def isPrime(a):
    try:
        if a%1 == 0 and a%2!=0 and a%3 !=0 and a%5 !=0 and a%7!=0:
            return True
        else:
            return False
    except:
        print('pls input a intger')
print(isPrime(123))
"""
#5-3记数函数
"""
def count(s):
    l = len(s)
    count_int,count_str,count_sp,count_or = 0,0,0,0
    for i in range(l):
        if '0'<=s[i]<='9':
            count_int +=1
        elif 'A'<=s[i]<='z':
            count_str +=1
        elif s[i]==' ':
            count_sp +=1
        else:
            count_or +=1
    print('数字、字母、空格、其他分别为:',count_int,count_str,count_sp,count_or)

count('1232h1 2huuusnu2 u1ue2ue #$ ')
"""

猜你喜欢

转载自blog.csdn.net/wangxingfan316/article/details/83932856