-3

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

Blckknght
  • 100,903
  • 11
  • 120
  • 169
devjc
  • 73
  • 8
  • Nothing in your code explains the issue you're having. Could there have been a carriage return character ('\r') at the end of the first answer you gave? If you pasted it in from somewhere, it could explain the issue. Try printing the `repr` of the two strings, rather than (unnecessarily) calling `str` on them. – Blckknght Mar 18 '23 at 00:28
  • @blckknight: ty for formatting - i did try to do some of it; appreciated! – devjc Mar 18 '23 at 00:29
  • Can you clarify whether you have a problem at all? Because your post suggests that by updating to Python 3, of which 3.8 is the olderst still-supported version of Python (Python 2.7 was discontinued _three years ago_ after a two year grace period), you solved your problem. Which means there is no reason to keep this post. – Mike 'Pomax' Kamermans Mar 18 '23 at 00:29
  • For debug in the failing case, replace the prints with `print repr(city)` and then `print repr(pet)`. That should give the exact text of the raw inputs. And since 2.7 `print` is a keyword, it would rule out some odd `print` functino. – tdelaney Mar 18 '23 at 00:33
  • @Mike'Pomax'Kamermans I fail to understand why trying to troubleshoot an issue with an outmoded version of Python isn't a good idea. It was the version I had initially installed and I had not understood why it was an issue - hence the post. If the goal is to suppress outmoded versions - then please delete this post. I was not trying to make waves - but learn something that I didn't understand. – devjc Mar 18 '23 at 00:37
  • Mostly because Python 2.7 is no longer a thing. If you installed it for this exercise, you installed the wrong version, and the first step in problem solving should indeed be installing the correct version of Python and seeing if the problem still exists. Figuring out why Python 2.7 behaves the way it does is not going to help any future visitors, and won't help you either, since you should not continue to use 2.x for this exercise, you should be using Python 3.8 or newer. – Mike 'Pomax' Kamermans Mar 18 '23 at 00:38

1 Answers1

0

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.

devjc
  • 73
  • 8