Reputation: 21
I'm looking to create a little game in Python 3 where, when you answer the question with an input, the code automatically adds something right after you on the same line :
1 print("Francis - Hello Stranger ! What's your name ?")
2 name = input("??? - My name is ")
3 print(f"Francis - Oh, hello {name}. You need to go South.")
So here, I'll just write my name "Arnaud" and I would like that the program adds automatically the sentence ("and I need to go in town.") right after "Arnaud" in line 2 (after my input).
It should looks like:
1 Francis - Hello Stranger ! What's your name ?
2 ??? - My name is ***Arnaud*** and I need to go in town. #Here, I just write my name "Arnaud"
3 Francis - Oh, hello Arnaud. You need to go South.
But I don't understand how to add an string or else right after an input on the same line. I tried with the print("Something", end="") fUnction but didn't work.
1 print('The cat do "Mi-', end="")
2 print('AOU!"')
3
4 print('I\'am thinking of : ', end="")
5 internalThought = input ("", end="")
6 print(".")
Upvotes: 2
Views: 1688
Reputation: 44323
This won't work in Windows, but should work on Linux/Unix.
print("Francis - Hello Stranger ! What's your name ?")
name = input("??? - My name is ")
print(f"\033[A??? - My name is ***{name}*** and I need to go in town.")
print(f"Francis - Oh, hello {name}. You need to go South.")
\033[A
is the character sequence to move the cursor up one line.
Demo:
>>> def foo():
... print("Francis - Hello Stranger ! What's your name ?")
... name = input("??? - My name is ")
... print(f"\033[A??? - My name is ***{name}*** and I need to go in town.")
... print(f"Francis - Oh, hello {name}. You need to go South.")
...
>>> foo()
Francis - Hello Stranger ! What's your name ?
??? - My name is ***Arnaud*** and I need to go in town.
Francis - Oh, hello Arnaud. You need to go South.
On Windows (not so good):
>>> def foo():
... print("Francis - Hello Stranger ! What's your name ?")
... name = input("??? - My name is ")
... print(f"\033[A??? - My name is ***{name}*** and I need to go in town.")
... print(f"Francis - Oh, hello {name}. You need to go South.")
...
>>> foo()
Francis - Hello Stranger ! What's your name ?
??? - My name is Arnaud
←[A??? - My name is ***Arnaud*** and I need to go in town.
Francis - Oh, hello Arnaud. You need to go South.
>>>
chepner's comment about exploring curses
is on point if you need to get this to work on Windows.
I just discovered that curses
is not supported under Windows.
Explanation
Writing \033[A
to the output moves the cursor up one line back to where the input
statement was executed. Rather that figure how to now space over to the end of ??? - My name is
to write the rest of what needs to be written (that is, ***{name} and I need to go in town.
), we just rewrite ??? - My name is
, which has the same effect as spacing over that string.
Upvotes: 2
Reputation: 156
You cannot print a string immediately after the input() function on the same line in python3. There are complex workarounds to build custom solutions, but you would need to rewrite the source code for the input() function, therefore not using the input() function.
Upvotes: 0
Reputation: 43
Hey you can do this way !
print (" ?? - My name is {0} and I need to go in town " .format(input()))
Upvotes: 0