摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載
刷題繼續(xù)
昨天和大家分享了41-50題,今天繼續(xù)來刷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:
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 pointQuestion 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上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載:
Python 51-60題
我的運行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點:
raw_input()在Python3中是input()
print需要加括號
fstring可以換成.format(),或者%s,%d
謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評論里,我們一起進步!!!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/43967.html
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題 刷題繼續(xù) 昨天和大家分享了51-60題,今天繼續(xù)來刷61~70題 Question 61: The Fibonacci Sequence is computed based o...
摘要:笨辦法學第版結(jié)構(gòu)非常簡單,共包括個習題,其中個覆蓋了輸入輸出變量和函數(shù)三個主題,另外個覆蓋了一些比較高級的話題,如條件判斷循環(huán)類和對象代碼測試及項目的實現(xiàn)等。最后只想說,學習不會辜負任何人,笨辦法學 內(nèi)容簡介 《笨辦法學Python(第3版)》是一本Python入門書籍,適合對計...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題我的運 刷題繼續(xù) 昨天和大家分享了71-80題,今天繼續(xù)來刷81~90題 Question 81: By using list comprehension, p...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下 刷題繼續(xù) 昨天和大家分享了61-70題,今天繼續(xù)來刷71~80題 Question 71: Please write a program to out...
閱讀 2902·2023-04-26 02:14
閱讀 3751·2019-08-30 15:55
閱讀 1843·2019-08-29 16:42
閱讀 2757·2019-08-26 11:55
閱讀 2846·2019-08-23 13:38
閱讀 480·2019-08-23 12:10
閱讀 1308·2019-08-23 11:44
閱讀 2790·2019-08-23 11:43