print("This program will calculate the numeric value of a name given as input.")
name = input("Please enter your full name: ")
name_list = name.split(' ')
name_list2 = []
for x in name_list:
y = list(x)
for x in y:
name_list2.append(x)
print(name_list2)
num_value = 0
for x in name_list2:
y = ord(x)
print("The numeric value of", x, "is", y)
num_value = num_value + y
print("The numeric value of your name is: ", num_value)
Any tips on how to simplify this is appreciated, with my knowledge I couldn't see an easier way to split the list, split out each character (to avoid adding in the whitespace value of 32), and then add them up.
You can iterate over the name and sum the ord's of each character excluding spaces from the count with if not ch.isspace():
name = input("Please enter your full name: ")
print("The numeric value of your name is: ", sum(ord(ch) for ch in name if not ch.isspace()))
If you want to see each letter use a for loop:
name = input("Please enter your full name: ")
sm = 0
for ch in name:
if not ch.isspace():
y = ord(ch)
print("The numeric value of", ch, "is", y)
sm += y
print("The numeric value of your name is: ", sm)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With