为什么 print() 函数不能打印我在 Windows 中要求的内容?

发布于 2024-10-28 16:29:19 字数 1103 浏览 0 评论 0原文

只是为了好玩,我正在 Python 3.2 中构建一个非常简单的基于文本的游戏。我的开发计算机运行的是 Windows 7,并且我使用 PyScripter IDE 来开发此游戏。

我遇到了 print() 函数无法正确打印字符串的小问题。当您开始游戏时,程序会显示一个小菜单,其中包含 3 个选项:登录注册退出。当我去注册时,程序从我创建的名为 Character 的自定义类中创建一个对象。该角色接收一个 name 变量,然后使用该 name 变量打印出一条小消息。这是我必须执行此操作的代码:

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")

代码应该正常工作,当我在 PyScripter 中运行程序时,我会得到一个很好的输出:

我明白了,所以你的名字是(我选择的名字)...很好。我们将立即开始您的旅程!

但是,当我从 cmd 提示符运行该程序时,我得到:

...很好。我们将立即开始您的旅程!

我在这里做错了什么吗?或者这是 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 wrong here? Or is this a fault on Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-11-04 16:29:19

我相信您的 print() 语句中有一个拼写错误:

# 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")

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")
疯到世界奔溃 2024-11-04 16:29:19

以下内容在带有 Eric5 IDE 的 Python 3.2 中工作:

heroname = "Joe Cool"
print("I see, so your name is", heroname + "... 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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文