美文网首页
Python 100练习题 Day4

Python 100练习题 Day4

作者: P酱不想说话 | 来源:发表于2021-03-17 11:38 被阅读0次

Question 14

Question: Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.

Suppose the following input is supplied to the program:

Hello world!
Then, the output should be:

UPPER CASE 1 LOWER CASE 9
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.

upper,lower = 0,0
for i in x:
    if 'a'<=i and i<='z':
        lower = lower + 1
    if 'A'<=i and i<='Z':
        upper = upper +1
#or use the function belowing:
# for i in x:
#     lower+=i.islower()
#     upper+=i.isupper()

print("lower:",lower)
print("upper:",upper)

summary:

这一部分主要集中在列表的遍历和字符的判断,islower()和iupper()两个方法算是比较常用的两个方法,可以直接记忆

Question 15

Question:
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.

Suppose the following input is supplied to the program:

9
Then, the output should be:

11106
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

def compute(num):
    return num+(10*num+num)+(100*num+10*num+num)+(1000*num+100*num+10*num+num)
compute(9)

没什么难度的题,也没什么好说的

相关文章

网友评论

      本文标题:Python 100练习题 Day4

      本文链接:https://www.haomeiwen.com/subject/ikbscltx.html