2
3

数値変換判定処理

Last updated at Posted at 2024-01-30

数値に変換できるかチェックできるプログラムを2通りで実装しました。

1.例外処理を使う場合

str1 = input()
try:
    num1 = int(str1)
    print("YES")
except:
    print("NO")

2.isdigitメソッドを使う場合

s = input()

if s.isdigit():
    print("YES")
else:
    print("NO")

追加
正負記号を入れた整数はisdigitメソッドでは判定できません
下のプログラムを実行しました。

タイトルなし.jpg

2
3
2

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
2
3