Converting String to Ordinal Value

雨燕双飞 提交于 2019-12-03 01:13:30

问题


I'm looking to compute the sum of the ordinal values of all the characters of the full name string, and will output this sum.

here is what I have so far.

a = input('Enter your first name: ')
b = input('Enter your last name: ')
c = print("your full name is:", a, b)
print(ord(a))

so for example if you put Mary for the first name and Joe for the last name the full name would be Mary Joe and the ordinal value would be 727.


回答1:


If you only concatenate a and b you will get 695:

print(sum(ord(i) for i in a+b))
# 695

But, it seems you also need a space between the first and last name:

print(sum(ord(i) for i in '{0} {1}'.format(a, b)))
# 727



回答2:


Simply use a for loop like so num=sum([ord(i) for i in c],0) assuming c is "Mary Joe" or the string you want. Also, you should use raw_input if you want the user to give you a string, and for printing use print "your full name is: " + c



来源:https://stackoverflow.com/questions/36595400/converting-string-to-ordinal-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!