Public Group

# python problem so lost .

This topic is 4191 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay im learning python from a book and at the end of each chapter there are practice problems. this was the chapter on strings. ill start off by saying the problem then ill say what i know and why i am confused. hopefully someone can give me a tip or a hint pointing me in the right direction. Numerologists claim to be able to determine a person's character traits based on the "numeric value" of a name. The value of a name is determined by summing up the values of the letters of the name where 'a' is 1, 'b' is 2, 'c' is 3etc., up to 'z' being 26. For example, the name "Zelle" would have the value 26 + 5 + 12 + 12 + 5 = 60 (which happens to be a very auspicious number, by the way). Write a program that calculates the numeric value of a singler name provided as input. okay so far ive written example programs that are like this but the exact opposite. i made one where you would type in the number then it would look up on a list the corresponding month to the number. it would use indexing/slicing all that stuff. i also made a program where it made a list of grades you would type in a value 1-100 and it would spit out the letter grade a b c d f thanks to the info from someone on this board i created the list in parts by f, d, c, b, a by doing F = ["f" for x in range(60)]. so now my dilemma I have to have the user type in letters and then have my program look up the values add them up and spit it back out. so i know im going to probably need to loop through each letter right? so for ch in name: that would be if the thing the user entered was name. the thing that just screws with my mind is how do i associate the letters with those numbers? because in my last programs a number was entered then i could just do listvariable[number] and find it on the list. but this one i need to make a list 1-26 right but how do i associate the letter with the value? thats what really confuses me i know you dont do ord(), thats what i first though then i realised thats the wrong value its 1-26 not the ascii things.(previously in this chapter we made a ascii decoder and encoder). okay well if anyone can help and give me a tip or idea on how to accomplish this thank you very much. hey ho

'a' - 'a' = 0
'b' - 'a' = 1
'c' - 'a' = 2
etc..

##### Share on other sites
Quote:
 Original post by christian h'a' - 'a' = 0'b' - 'a' = 1'c' - 'a' = 2etc..

thankyou but the thing is we havent learned anything like that yet. is there anyway to accomplish this with manipulating strings? also could you explain what you showed me in more depth since i havent learned it yet. so a - a = 0 ? i actually need a to equal 1 but thank you. why does a repeat so many times? is that - mean minus? so what does a equal if a - a is 0 and b - a = 1?

thanks
hey

##### Share on other sites
You've got three options:

1) You could use the ASCII code to determine the value of the character
2) You could use a lot of if and elif statements to manually determine the value
3) You could use a dictionary to store a code for each character

Building a dictionary and using a lot of ifs and elifs will be time consuming. Using ASCII codes requires that you use an in-built function ord(). First you need the string to be either all uppercase or all lowercase (this simplifies the problem greatly). Then you get the ASCII codes of each character in the string and subtract the ASCII code of the first letter (either 'A' or 'a' depending on if you're using capital letters or lowercase letters):

for char in name.upper():    total += ord(name) - ord('A')

##### Share on other sites
Quote:
 Original post by CrimsonSunYou've got three options: 1) You could use the ASCII code to determine the value of the character2) You could use a lot of if and elif statements to manually determine the value3) You could use a dictionary to store a code for each characterBuilding a dictionary and using a lot of ifs and elifs will be time consuming. Using ASCII codes requires that you use an in-built function ord(). First you need the string to be either all uppercase or all lowercase (this simplifies the problem greatly). Then you get the ASCII codes of each character in the string and subtract the ASCII code of the first letter (either 'A' or 'a' depending on if you're using capital letters or lowercase letters):for char in name.upper(): total += ord(name) - ord('A')

i think i would have to do 2 or 3 because its not the ascii code that the problem is looking for. you need to have them equal 1-26. having A being 1 since its the first letter in the alphabet and z being 26. so would my only solution be 2nd or 3rd? if so how would i approach doing the 3rd since in my book the 2nd one is a chapter about 2 more from where i am now.

thanks
hey

##### Share on other sites
You aren't getting the just the ASCII codes -- you are getting them in order to determine the value of the character: my code is essentially the same as the pseudocode that christian h posted above. If you need 'A' to have a value of one, change the code I posted to:
for char in name.upper():    total += ord(name) - ord('A') + 1

##### Share on other sites
Quote:
 Original post by HeyHoHeythe thing that just screws with my mind is how do i associate the letters with those numbers?

A letter is a number. No, seriously.

Computers only deal with numbers, really, so to make them deal with any other type of data we have to trick them into considering numbers as something else. For example, to make computers deal with letters, we devised a code called ASCII that represents letters by various numbers in the range [0 - 127]. Why the numbers are in that range is unimportant for now, as is the fact that there are other codes for handling letters besides ASCII. What is important is that, by default, your common alphabetic characters [a-z] and [a-Z] have numerical values that are not only less than or equal to 127, but sequential.

The fact that their values are sequential is very important, and very useful. If the value of 'a' is x and the value of 'b' is x + 1, then the value of the nth lowercase letter of the alphabet is x + (n - 1). Let's validate that algorithm:

'a' is the first letter. The value of the 1st letter is: x + (1 - 1) → x + 0 → x
'b' is the second letter. The value of 'b' is: x + (2 - 1) → x + 1
'j' is the tenth letter. The value of 'j' is: x + (10 - 1) → x + 9

So if we assign the value of 1 to 'a', we can determine the value of any letter based on its distance from 'a'. But how do we calculate that distance? Well, remember that we pointed out that the numerical values of letters in our ASCII code scheme are sequential? So we can simply subtract their numerical values from each other:
'j' - 'a' = 9
'b' - 'a' = 1
'a' - 'a' = 0

(This is what christian h was pointing out to you)

Combining that with your construct to iterate over the characters in a string:
for ch in name:  distance = ch - 'a'  value = distance + 1  # value of 'a' = 1

which we can condense to:
for ch in name:  value = (ch - 'a') + 1

I know. ch is a letter, not a number! To retrieve the numerical value of a letter in Python, we use the ord() builtin function, as Crimson Sun showed you. So the code becomes:
for ch in name:  value = ord(ch) - ord('a') + 1

Based on this, I'm sure you can finish the assignment. Just keep in mind that uppercase letters have lower values than lowercase (ord('A') → 65; ord('a') → 97). You might therefore want to convert the names entirely to lowercase first.

Happy hacking!

##### Share on other sites
Quote:
Original post by Oluseyi
Quote:
 Original post by HeyHoHeythe thing that just screws with my mind is how do i associate the letters with those numbers?

A letter is a number. No, seriously.

Computers only deal with numbers, really, so to make them deal with any other type of data we have to trick them into considering numbers as something else. For example, to make computers deal with letters, we devised a code called ASCII that represents letters by various numbers in the range [0 - 127]. Why the numbers are in that range is unimportant for now, as is the fact that there are other codes for handling letters besides ASCII. What is important is that, by default, your common alphabetic characters [a-z] and [a-Z] have numerical values that are not only less than or equal to 127, but sequential.

The fact that their values are sequential is very important, and very useful. If the value of 'a' is x and the value of 'b' is x + 1, then the value of the nth lowercase letter of the alphabet is x + (n - 1). Let's validate that algorithm:

'a' is the first letter. The value of the 1st letter is: x + (1 - 1) → x + 0 → x
'b' is the second letter. The value of 'b' is: x + (2 - 1) → x + 1
'j' is the tenth letter. The value of 'j' is: x + (10 - 1) → x + 9

So if we assign the value of 1 to 'a', we can determine the value of any letter based on its distance from 'a'. But how do we calculate that distance? Well, remember that we pointed out that the numerical values of letters in our ASCII code scheme are sequential? So we can simply subtract their numerical values from each other:
'j' - 'a' = 9
'b' - 'a' = 1
'a' - 'a' = 0

(This is what christian h was pointing out to you)

Combining that with your construct to iterate over the characters in a string:
for ch in name:  distance = ch - 'a'  value = distance + 1  # value of 'a' = 1

which we can condense to:
for ch in name:  value = (ch - 'a') + 1

I know. ch is a letter, not a number! To retrieve the numerical value of a letter in Python, we use the ord() builtin function, as Crimson Sun showed you. So the code becomes:
for ch in name:  value = ord(ch) - ord('a') + 1

Based on this, I'm sure you can finish the assignment. Just keep in mind that uppercase letters have lower values than lowercase (ord('A') → 65; ord('a') → 97). You might therefore want to convert the names entirely to lowercase first.

Happy hacking!

okay thank you everyone for your posts after reading them then reading yours oluseyi you got my brain to click lol. i finally understand the logic behind your code as well as the code. i never realised the fact that the ascii code is sequential was so important and useful. just to make sure let me say in my own words what i think the code means.
for ch in name:
value = ord(ch) - ord('a') + 1
so this goes through each ch in the name then it takes the value of the character minus the value of a + 1
the reason that it does that is minusing the ch from a finds the distance between a and the ch since their in sequential order you can do that. and then the + 1 just adds 1 because otherwise a would have the value of 0 and b 1 etc and i need to make a 1 and b 2 etc. i think with the help of all of your posts i decently understand it, now im going to write the program thanks everyone.

hey ho hey

##### Share on other sites
Well, you've still got a problem with your code. If you're going to use ord() then you need to make sure that you don't count capital letters and lowercase letters differently. Also, what happens when someone puts an underscore or a digit into their name? Do you count those digits as part of the score or do you ignore them? I would use some of string member functions such as upper(), lower(), and isalpha() in order to deal with these factors.

##### Share on other sites
Quote:
 Original post by CrimsonSunWell, you've still got a problem with your code. If you're going to use ord() then you need to make sure that you don't count capital letters and lowercase letters differently.

Valid: The code should be case-insensitive.

@HeyHoHey:
Converting a string to lowercase in Python is trivial: str.lower(). Testing if a character is lowercase is similarly trivial: str.islower().

Quote:
 Also, what happens when someone puts an underscore or a digit into their name?

Valid: Good code is error-tolerant.

@HeyHoHey:
You'll need to decide how you want your code to behave when it encounters a non-letter character.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633685
• Total Posts
3013320
×