6 Replies - 1373 Views - Last Post: 19 May 2011 - 09:53 AM Rate Topic: -----

#1 E-Thug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 11

How to process userinput strings or integers?

Posted 18 May 2011 - 05:55 PM

I am building a basic numerology calculator. I want to know how to take in the raw_input(), process it via a characterdict which looks like

def Main(name):
  #generating a lettermap.
  CharacterMapDict {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':1,'k':2,'l':3,'m':4,'n':5,'o':6,'p':7,'q':8,'r':9,'s':1,'t':2,'u':3,'v':4,'w':5,'x':6,'y':7,'z':8}



This is supposed to map the letters to numbers in the dictionary. The user is supposed to enter their name via Raw_input() or Input(), or whichever method can be used. The name is supposed to be parsed, turned into numbers, and then the numbers crunched by the digital sum function. This function should produce a value, and then I want each value to be mapped to or associated with a print("words..words..").

I figure for the print stuff I could use some type of loop, similar to c's case statements or if statements.

Am I on the right track?


This is only my second python program so I'm not all that experienced but I figure it would be challenging enough.

Any suggestions on how to get this part to work would be appreciated.

This post has been edited by E-Thug: 18 May 2011 - 05:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to process userinput strings or integers?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,543
  • Joined: 23-August 08

Re: How to process userinput strings or integers?

Posted 19 May 2011 - 03:30 AM

Hint: a string is a sequence of characters over which you can easily iterate.
Was This Post Helpful? 1
  • +
  • -

#3 E-Thug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 11

Re: How to process userinput strings or integers?

Posted 19 May 2011 - 08:01 AM

View PostJackOfAllTrades, on 19 May 2011 - 03:30 AM, said:

Hint: a string is a sequence of characters over which you can easily iterate.


That is exactly what I'm trying. Example:
for character in "test":
    print character
>>>
t
e
s
t



So I know I'm going to have to use for character in "string" or in name.

I tried:
  Main = raw_input("name:  ")
  n = 0
  for character in n:
    n += LetterMapDict[character]
    return n
>>>
>>>
>>>



No result but apparently no bugs. This code is supposed to take in the raw_input "string" and evaluate
each character while starting with the value of 0, using the LetterMapDict to give a value to each character.

I tried:

  name = 0
  name = raw_input("name:  ")
  for character in name:
    name += LetterMapDict[character]
    return name
>>>
>>>
>>>



Nothing happens. So somewhere I'm either not using raw_input correctly, or something is missing. The interpreter isn't giving me any bug messages, any errors, it just runs but doesn't give me any indication of what
exactly is wrong.

Basically I want to both print the result of name to the user, and then take that value to use to
print the preset "strings" using the if/elif or something similar.

I've figured out how to do the preset using if elif part. So I'm just stuck here.
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How to process userinput strings or integers?

Posted 19 May 2011 - 08:11 AM

Your indentation is off, the return statement should be unindented. Also, you'll probably want to rename your total to something that makes more sense... like total :)

  total = 0
  name = raw_input("name:  ")
  for character in name:
    total += LetterMapDict[character]
  return total


This post has been edited by atraub: 19 May 2011 - 08:12 AM

Was This Post Helpful? 1
  • +
  • -

#5 E-Thug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 11

Re: How to process userinput strings or integers?

Posted 19 May 2011 - 08:37 AM

View Postatraub, on 19 May 2011 - 08:11 AM, said:

Your indentation is off, the return statement should be unindented. Also, you'll probably want to rename your total to something that makes more sense... like total :)

  total = 0
  name = raw_input("name:  ")
  for character in name:
    total += LetterMapDict[character]
  return total




Here is what I tried:
def Main(name):
  #generating a lettermap.
  LetterMapDict={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,
                 'j':1,'k':2,'l':3,'m':4,'n':5,'o':6,'p':7,'q':8,'r':9,
                 's':1,'t':2,'u':3,'v':4,'w':5,'x':6,'y':7,'z':8}

  total = 0
  name = raw_input("name:  ")
  for character in name:
    total += LetterMapDict[character]
  return total
>>>
>>>
>>>



Not much happened but is the code working? How can I check to see that the information is in total?

What I expected to happen is for raw_input to show a prompt with "name: ", the user enters his or her name, and it gets processed. For some reason that is not what happens.

This post has been edited by E-Thug: 19 May 2011 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How to process userinput strings or integers?

Posted 19 May 2011 - 09:35 AM

Firstly, you have Main(name) and name = raw_input()... is the user meant to enter the name twice the same? The second call will overwrite the first.

The "problem" that you are having is you are not printing any value at all, you returned it - but to where?

To see your changes either print the value in the function call or print the value returned:

Cleaned up a bit:

def Main():
  #generating a lettermap.
  LetterMapDict={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,
                 'j':1,'k':2,'l':3,'m':4,'n':5,'o':6,'p':7,'q':8,'r':9,
                 's':1,'t':2,'u':3,'v':4,'w':5,'x':6,'y':7,'z':8}
  total = 0
  name = raw_input("name:  ")
  for character in name:
    total += LetterMapDict[character]
  return total

myTotal = Main()
print myTotal

....

name: simown
30



I'd suggest renaming the function to what it actually does instead of the vague notion of a "main" function, which is normally used as an entry point to a bigger program.


Edit: After that you may want to think about error handling, what if someone enters a captial letter or a number, or nothing at all?

This post has been edited by Simown: 19 May 2011 - 09:37 AM

Was This Post Helpful? 1
  • +
  • -

#7 E-Thug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 11

Re: How to process userinput strings or integers?

Posted 19 May 2011 - 09:53 AM

View PostSimown, on 19 May 2011 - 09:35 AM, said:

Firstly, you have Main(name) and name = raw_input()... is the user meant to enter the name twice the same? The second call will overwrite the first.

The "problem" that you are having is you are not printing any value at all, you returned it - but to where?

To see your changes either print the value in the function call or print the value returned:

Cleaned up a bit:

def Main():
  #generating a lettermap.
  LetterMapDict={'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,
                 'j':1,'k':2,'l':3,'m':4,'n':5,'o':6,'p':7,'q':8,'r':9,
                 's':1,'t':2,'u':3,'v':4,'w':5,'x':6,'y':7,'z':8}
  total = 0
  name = raw_input("name:  ")
  for character in name:
    total += LetterMapDict[character]
  return total

myTotal = Main()
print myTotal

....

name: simown
30



I'd suggest renaming the function to what it actually does instead of the vague notion of a "main" function, which is normally used as an entry point to a bigger program.


Edit: After that you may want to think about error handling, what if someone enters a captial letter or a number, or nothing at all?


Thank you. This was very informative.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1