资讯专栏INFORMATION COLUMN

Python基础练习100题 ( 51~ 60)

岳光 / 1882人阅读

摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载

刷题继续

昨天和大家分享了41-50题,今天继续来刷51~60题

Question 51:
Write a function to compute 5/0 and use try/except to catch the exceptions.

解法一
def divide():
    return 5/0

try:
    divide()
except ZeroDivisionError as ze:
    print("Why you are dividing a number by ZERO!!")
except:
    print("Any other exception")
Question 52:
Define a custom exception class which takes a string message as attribute.

解法一
class CustomException(Exception):
    """Exception raised for custom purpose
    Attributes:message -- explanation of the error
    """

    def __init__(self, message):
        self.message = message

num = int(input())
try:
    if num < 10:
        raise CustomException("Input is less than 10")
    elif num > 10:
        raise CustomException("Input is grater than 10")
except CustomException as ce:
    print("The error raised: " + ce.message)
Question 53:
Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.

Example:
If the following email address is given as input to the
program:

john@google.com
Then, the output of the program should be:
john
In case of input data being supplied to the question, it should be assumed to be a console input.

解法一
def getname(s):
    try:
        return s.split("@")[0]
    except:
        print("This is not a email-adress")

getname("john@google.com")
解法二
import re

email = "john@google.com"
pattern = "(w+)@w+.com"
ans = re.findall(pattern,email)
print(ans)
Question 54:
Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.

Example:
If the following email address is given as input to the program:

john@google.com
Then, the output of the program should be:
google
解法一
import re

email = "john@google.com"
pattern = "w+@(w+).com"
ans = re.findall(pattern,email)
print(ans)
解法二
def getname(s):
    try:
        return s.replace("@",".").split(".")[1]
    except:
        print("Something Wrong")

getname("john@google.com")
Question 55:
Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.

Example:
If the following words is given as input to the program:

2 cats and 3 dogs.
Then, the output of the program should be:
["2", "3"]
In case of input data being supplied to the question, it should be assumed to be a console input.

解法一
import re
s = input()
print(re.findall("d+",s))
解法二
import re
s= input()
pattern = "d+"
ans = re.findall(pattern,s)
print(ans)
解法三
[int(s) for s in input().split() if s.isdigit()]
Question 56:
Print a unicode string "hello world".

解法一
unicodeString = u"hello world!"
print(unicodeString)
Question 57:
Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.

解法一
s = input()
u = s.encode("utf-8")
print(u)
Question 58:
Write a special comment to indicate a Python source code file is in unicode.

解法一
# -*- coding: utf-8 -*-
Question 59:
Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).

*Example:
If the following n is given as input to the program:*

5
Then, the output of the program should be:
3.55
解法一
n = int(input())
sum = 0
for i in range(1, n+1):
    sum+= i/(i+1)
print(round(sum, 2))  # rounded to 2 decimal point
Question 60:
Write a program to compute:
f(n)=f(n-1)+100 when n>0
and f(0)=1
with a given n input by console (n>0).

Example:
If the following n is given as input to the program:

5
Then, the output of the program should be:
501
解法一
def f(n):
    if n == 0:
        return 1
    else:
        return f(n-1) + 100

n = int(input())
print(f(n))
源代码下载

这十道题的代码在我的github上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:

Python 51-60题

我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

raw_input()在Python3中是input()

print需要加括号

fstring可以换成.format(),或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/43967.html

相关文章

  • Python基础练习100 ( 61~ 70)

    摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题 刷题继续 昨天和大家分享了51-60题,今天继续来刷61~70题 Question 61: The Fibonacci Sequence is computed based o...

    jeyhan 评论0 收藏0
  • 测试开发必看:《笨办法学Python3》PDF中文高清版,豆瓣高分8.0

    摘要:笨办法学第版结构非常简单,共包括个习题,其中个覆盖了输入输出变量和函数三个主题,另外个覆盖了一些比较高级的话题,如条件判断循环类和对象代码测试及项目的实现等。最后只想说,学习不会辜负任何人,笨办法学 内容简介   《笨办法学Python(第3版)》是一本Python入门书籍,适合对计...

    不知名网友 评论0 收藏0
  • Python基础练习100 ( 81~ 90)

    摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运 刷题继续 昨天和大家分享了71-80题,今天继续来刷81~90题 Question 81: By using list comprehension, p...

    刘德刚 评论0 收藏0
  • Python基础练习100 ( 71~ 80)

    摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下 刷题继续 昨天和大家分享了61-70题,今天继续来刷71~80题 Question 71: Please write a program to out...

    Jeff 评论0 收藏0
  • 【万人千】结对编程排位赛(第一期) 第二周 排名公布,冠军成功卫冕,啊这……

    摘要:万人千题结对编程排位赛如果想参加的第二期的同学,可以先联系作者加群,看看第一期的同袍是如何奋斗的。   博主会带领大家进行 《C语言入门100例》 和 《算法零基础...

    PumpkinDylan 评论0 收藏0

发表评论

0条评论

岳光

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<