Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Python program to create numeric values based on Unicode values, would like tips to streamline my code

Tags:

python

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.

like image 774
flybonzai Avatar asked Dec 14 '25 14:12

flybonzai


1 Answers

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)
like image 157
Padraic Cunningham Avatar answered Dec 16 '25 04:12

Padraic Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!