卷上卷队——钟某人刷题的day4——python100例

卷上卷队——钟某人刷题的day4——python100例,第1张

卷上卷队——钟某人刷题的day4——python100例

第十二题~

将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5

这一题的思路可以这样: 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5 ''' ''' 首先我们假设一个数a,for i in range(2,a+1) 如果在这个期间有可以整除a的数,记录下来,停止循环,记录模 对模无限循环上述过程,直到i == 模

别看思路挺简单的,为了这个我debug了整整一个下午啊T.T,不多说了,上代码

number = int(input('请输入你想查询的数字'))
final = number
remainders = 1
remainder = []
numbers = 0
while numbers != remainders:
    for i in range(2,number+1):
        numbers = i
        if number%i==0:
            if numbers == remainders:
                break
            else:
                remainders = number/i
                number = int(remainders)
                remainder.append(str(i))
                break
        else:
            pass
remainder.append(str(number))
print(f'{final}={"*".join(remainder)}')
 

第十三题~

利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示

挺简单的,代码走起~

def score(score):
    if score >= 90:
        return 'A'
    elif 60 <= score <= 89:
        return 'B'
    else:
        return 'C'
print(f"该学生的分数等级是{score(int(input('请输入分数')))}")

第十四题~

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

这里我第一时间想到的就是for+isdigit,isspace,isalpha,分别统计,不多说了,上代码就完了

str = input('enter you words:n')
alpha = 0
space = 0
digit = 0
other = 0
for i in str:
    if i.isalpha():
        alpha += 1
    elif i.isspace():
        space += 1
    elif i.isdigit():
        digit += 1
    else:
        other += 1
print(f'alpha have {alpha}n'
      f'space have {space}n'
      f'digit have {digit}n'
      f'other have {other}n')

现在我们来看看他的效果怎么样吧!

D:SoftWorkSpacepythonpython.exe D:/WorkSpace/py_case/homework.py
enter you words:
python is the best language in this world!
alpha have 34
space have 7
digit have 0
other have 1
 
 
Process finished with exit code 0

嗯,效果拔群

第十五题~

求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制

意思就是我们将会有两个要输入的值,一个是a,还有一个是要加多少次,并不是特别难,开始吧!

times = int(input('how many times would you want to add:n'))
a = int(input('enter the number which you want to plus:n'))
s = 0
c = 0
for i in range(times):
    s += a*10**i
    c += s
print(c)

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5710669.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存