You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 2.7.6中实现输入与提示同行显示的代码修复请求

Fixing the Same-Line Input Prompt in Python 2.7.6

Got it, let's sort this out for you. The problem with your original code is that Python 2's print statement automatically adds a newline after outputting text—so your second prompt ends up on a new line instead of staying right next to the player's name. Here's the straightforward fix:

Working Code

player = raw_input("Enter player name: ")
# Use a formatted prompt directly in raw_input to keep everything on one line
column = int(raw_input("%s, choose a column: " % player))

Why This Works

  1. We first grab the player's name with raw_input() like you did before.
  2. Instead of using print for the second prompt, we pass a formatted string directly to raw_input(). This makes the prompt ([Player Name], choose a column: ) show up on the same line, and the user's input will follow right after it—exactly matching your desired output:

    Enter player name: Mark Mark, choose a column: 2

  3. We still convert the input to an integer with int() since we need a numeric column value.

Quick Side Note on input() vs raw_input()

Your original code used input() for the column, but in Python 2, input() evaluates user input as a Python expression (which can be risky). Using raw_input() and explicitly converting it to int() is the safer, standard approach here.

内容的提问来源于stack exchange,提问作者Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:14:41