# 计算一个数的平方根
import math
#t=input('输入一个数或者一个字母:')#字符与数字需要分开计算 input输入为字符型 需要做类型转换
#print(math.sqrt(ord(t)))#ord() expected a character, but string of length 2 found
#if type(t)==type(1):
def sqrtx(x):
if t.isdigit():#若输入为数字
# t1=float(t)
print('%c的平方根为%f'% (t,math.sqrt(float(t))))
elif t.isalpha():#若输入为字母
# t2=ord(t)#将字符转换为ASCII
print('%c的平方根为%f' % (t,math.sqrt(ord(t))))
else:
print('输入无效')
t=input('输入一个数或者一个字母:')#字符与数字需要分开计算 input输入为字符型 需要做类型转换
sqrtx(t)
相关函数介绍:
s为字符串
s.isalnum() 所有字符都是数字或者字母
s.isalpha() 所有字符都是字母
s.isdigit() 所有字符都是数字
s.islower() 所有字符都是小写
s.isupper() 所有字符都是大写
s.istitle() 所有单词都是首字母大写,像标题
s.isspace() 所有字符都是空白字符、\t、\n、\r
ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr()
函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值,或者 Unicode
数值,如果所给的 Unicode 字符超出了你的 Python 定义范围,则会引发一个 TypeError 的异常。
此函数只能转换一个字符的 若为ab这种就不行会报错
expected a character, but string of length 2 found