Search code examples
pythonstringcharacterjythonraw-input

JYTHON/PYTHON - raw_input losing first character of string


I am reading in a name as you will see below, however regardless of what you type in it loses the first character, for example, 'Bob' will become 'ob'. Because of this I cannot compare the user's input to what they are searching for, breaking the entire program.

def Find() :
  global AddressBookAll
  Success = false  
  PersonToFind = str(raw_input("\nEnter the nickname of the person you would like to find: "))
  for x in AddressBookAll :
    if(x == PersonToFind) :  
      success = true
      printNow("\n%s" %(x))
      for y in AddressBookAll[x] :
        printNow("%s" %(y))
  if(Success == false) :
    printNow("\nNot Found.")

Solution

  • There are a few problems with your code.

    First, you shouldn't be using a global variable. It would make much more sense to pass the address book to your function. Don't worry, only a reference will be passed, so it's cheap.

    Second, function names (and other variable names except classes) should be lowercase.

    Third, no need to call str() on raw_input() which already returns a string.

    Fourth, use the .format() string syntax instead of % string formatting.

    Fifth, x appears to be a string from AddressBookAll (that's a guess because you're comparing PersonToFind to it). Let's hope AddressBookAll is a dictionary, otherwise AddressBookAll[x] will fail.

    Sixth, if a single character is dropped anywhere, it must be in printNow() which you haven't shown to us. There is no other place in your code where something like that could happen.

    Seventh, case matters. True is not the same as true. Neither are Success and success.

    Eighth, Python is not Java. if doesn't need parentheses, for example.