昨天和你們分享了41-50題,今天繼續來刷51~60題python
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")
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)
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:git
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)
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:github
john@google.com
Then, the output of the program should be:
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")
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:less
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()]
Print a unicode string "hello world".
unicodeString = u"hello world!" print(unicodeString)
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)
Write a special comment to indicate a Python source code file is in unicode.
# -*- coding: utf-8 -*-
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:*ide
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
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:google
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上,若是你們想看一下每道題的輸出結果,能夠點擊如下連接下載:spa
個人運行環境Python 3.6+,若是你用的是Python 2.7版本,絕大多數不一樣就體如今如下3點:code
謝謝你們,咱們下期見!但願各位朋友不要吝嗇,把每道題的更高效的解法寫在評論裏,咱們一塊兒進步!!!orm