开发者

Why is the print() function not printing what I ask it to in Windows?

Just for fun, I'm building a very simple text-based game in Python 3.2. My development machine is running Windows 7, and I'm using the PyScripter IDE to develop this game.

I'm running into a slight problem with the print() function not printing a string out correctly. When you begin the game, the program displays a small menu with 3 choices: Login, Register, and Exit. When I go to register, the program creates an object from a custom class I made called Character. The character takes in a name variable, and then prints out a small message using the name variable. Here's the code that I have to do that:

user_choice = int(input("Please select a command: "))
if user_choice == 1:
    print("Sorry, this function isn't currently implemented.",
    "Please check back later.\n")
elif user_choice == 2:
    hero_name = input("Please choose a name for yourself, adventurer: ")
    hero = Character(hero_name)
    print("I see, so your name is", hero.name + "... Very well. We shall begin your journey immediately!\n")

The code should work properly, and when I run the program in PyScripter, I get a nice output:

I see, so your name is (the name I chose)... Very well. We shall begin your journey immediately!

But, when I run the program from the cmd-prompt, I get:

... Very well. We shall begin your journey immediately!

Am I doing something wron开发者_开发知识库g here? Or is this a fault on Windows?


I believe there's a typo in your print() statement:

# You have this:
print("I see, so your name is", hero.name + "... Very well. We shall begin your journey immediately!\n")

# Did you mean this?
print("I see, so your name is" + hero.name + "... Very well. We shall begin your journey immediately!\n")


The following works in Python 3.2 with Eric5 IDE:

heroname = "Joe Cool"
print("I see, so your name is", heroname + "... Very well. We shall begin your journey immediately!\n")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜