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
- We first grab the player's name with
raw_input()like you did before. - Instead of using
printfor the second prompt, we pass a formatted string directly toraw_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
- 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
相关产品推荐
相关产品推荐

