Why is my Python input overriding itself?

60 Views Asked by At

Context: I'm doing the Udemy "100-days-of-code" course and hit a weird stumbling block.
Python version: 2.7-64
I ultimately solved it by using the "input" command (vs raw_input) and upgrade the version of Python to 3.X.
The problem simply went away.
Thus, I didn't really solve the original problem. If you can help: What was I missing?

Scenario:

I used two raw_input() calls for input from the user. I then included the input in a summary.

Problem:

It is overriding the beginning of the summary for some reason.

Code:

# done this way to for input to be on the beginning
# yes I know I could have simply done city = raw_input("Birth city?\n"), etc
print("Birth city?")
city = raw_input()
print("Favorite pet?")
pet = raw_input()
print("Your band name would obviously NEVER be " + str(city) + " " + str(pet))

Output:

Birth city?
Angela
Favorite pet?
Lansbury
 Lansbury name would obviously NEVER be Angela

Expectation:

Birth city?
Angela
Favorite pet?
Lansbury
 Your band name would obviously NEVER be Angela Lansbury

Thanks for any clarification provided.


Supplemental attempt details:

I tried using the input command - but it didn't work for me. I suspect it's because I had 2.7 installed.

I tried storing both variables in a separate variable Iterating over this I found it retained only the first - dropping whichever i added second. (Which is also interesting - but doesn't explain why it was overriding the beginning

1

There are 1 best solutions below

0
devjc On

I'm not sure why what I posted doesn't include the issue. I thought I included the issue, the scenario, and the context.

However, full credit to @Blckknight for helping me see the problem soonest.

// Steps to resolve

  • I re-installed 2.7 and got the same issues.
  • I replaced the carriage return with an empty string for both variables.
  • And I re-ran without issue.

Thus, this was my solution:

#2. Ask the user for the city that they grew up in
city = raw_input("Birth city?\n").replace("\r", "")

#3. Ask the user for the name of a pet
pet = raw_input("Favorite pet?\n").replace("\r", "")

#4. Combine the name of their city and pet and show them their band name.
print("Your band name would obviously NEVER be " + str(city) + " " + str(pet))

Thanks for listening.