• 发表于 6个月前
• 阅读 3
• 收藏 0
• 评论 0

``````# -*- coding: UTF-8 -*-

"""
# Filename: is_numbers.py
# author by:  Sylvanas Windrunner
"""

# 1.判断是否为数字
def is_numbers(s):
try:
float(s)
return True
except ValueError as err:
print(err)

try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
print(TypeError, ValueError)

return False

# 2.判断输入的数字是否为奇数或者偶数
def odd_or_even(nums):
try:
if int(nums) % 2 == 1:
print('{} is even number.'.format(nums))
elif int(nums) % 2 == 0:
print('{} is odd number.'.format(nums))
else:
print('请输入一个正整数。')
except ValueError as err:
print(err)
return

# 测试字符串和数字
print(is_numbers('foo'))            # False
print(is_numbers('1'))              # True
print(is_numbers('1.3'))            # True
print(is_numbers('-1.37'))          # True
print(is_numbers('1e3'))            # True

print('====='*15)
# 测试Unicode
# 阿拉伯语 5
print(is_numbers('٥'))  # True
# 泰语 2
print(is_numbers('๒'))  # True
# 中文数字
print(is_numbers('四'))  # True
# 版权号

# 基数、偶数方法测试
odd_or_even('rr')  # False
odd_or_even('5')  # 奇数
odd_or_even(7)  # 奇数
odd_or_even(6)  # 偶数

``````

×