LoginSignup
0

More than 3 years have passed since last update.

ハッカーランクでの今週の学びをアウトプット(2)

Posted at

エラーハンドリング

try:
    print(int(S))#とりあえず実行。えらーが出ればexept以下実行
except:
    print("Bad String")

エラーハンドリング2

class Calculator(object):
    def power(self, n, p):
        if n < 0 or p < 0:
            raise Exception('n and p should be non-negative')#raise で投げるエラーを定義できる、Exceptionはシステム終了以外の全ての組み込み例外の親クラス
        return n**p


myCalculator=Calculator()
T=int(input())
for i in range(T):
    n,p = map(int, input().split())
    try:
        ans=myCalculator.power(n,p)
        print(ans)
    except Exception as e:
        print(e)   

文字列の反転

pythonの文字列は変更不可(タプルみたいな感じ)なので、反転するメソッドを使う時は、別物が生成されると考える

new_str = org_str[::-1]
print(new_str)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0