2

So I am a beginner to Python (previous experience with Java), and I was creating a simple OOP program. Here is my class:

from Tools.scripts.treesync import raw_input


class Info:

    def startGame(self):
        name = raw_input('What is your name?\n')
        age = raw_input("What is your age?\n")
        color = raw_input("What is your favorite color\n")
        print("Your name is %s, your age is %s, and your favorite color is %s" % (name, age, color))
        return Info


class App:

    def build(self):
        game_app = Info()
        game_app.startGame()
        return game

if __name__ == "__main__":
    obj = App()
    game = Info()
    obj.build()

This is the output:

Your name is Chris
, your age is 18
, Sand your favorite color is Red

I am confused as to why it is printing on 3 lines? Is there any way to print this onto a single line?

3
  • What is the difference between Tools.scripts.treesync.raw_input function and the standardlib raw_input (which should strip newline characters)? Commented Jul 12, 2017 at 21:11
  • 1
    None of what you posted is Javascript. Please don't post it as a Javascript snippet. Commented Jul 12, 2017 at 21:13
  • You should either use input instead of raw_input or use name.strip() to remove the trailing newline (also for the other variables) Commented Jul 12, 2017 at 21:16

2 Answers 2

2

The raw_input you're importing is keeping the CRLF in the input you entered. Why not use the built-in input() function (you are using Python 3, right)?

>>> from Tools.scripts.treesync import raw_input
>>> raw_input("> ")
> abc
'abc\n'
>>> input("> ")
> abc
'abc'

Also, Python is not Java. No need to put everything in classes.

Sign up to request clarification or add additional context in comments.

8 Comments

What is the reason for the downvotes? OP is not using the built-in raw_input() - there isn't one in Python 3, and the print() syntax he's using suggests that that's what he is on.
I was wondering the same (I didn't downvote, btw). Isn't this answer also correct?
Sorry, missed the raw_input import.
@Waldxn: A class should never be used as a holder for functions. Java only does that because of the inflexible structure of Java programs. The operations supported by instances of a class should be methods of the class, and some closely-related functions (such as alternate constructors) may be appropriate as classmethods or staticmethods, but if you just want somewhere to group a bunch of functions together, that's what modules are for.
Usually you store functions in modules (each Python file is a module). Adding a function to a class makes sense for object methods, i. e. methods that are only useful for a specific object. In your case the class doesn't do anything besides harboring a function - that's not how Python "works". It may be different with complex objects, but your question is a bit too broad for an answer in a comment :)
|
1

You are using raw_input, which includes a line ending to the string, you can just use input() to obtain the string without a line ending.

1 Comment

It's not the standard raw_input() but rather something imported from a library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.