2

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(".")
halfer
  • 19,824
  • 17
  • 99
  • 186
  • `input` is a fairly coarse tool: it writes a string to standard output, then reads from standard input. For what you want, you'll need something like `curses`. – chepner Jan 06 '20 at 16:05
  • Have a look [here](https://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line/41436173#41436173) – Phoenixo Jan 06 '20 at 16:26

3 Answers3

1

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.

Booboo
  • 38,656
  • 3
  • 37
  • 60
  • I'm cleary not at that level yet ! Thank you for your answer but I didn't understand your code ... Only the part that said on linux i could work since there's a command which can move the cursor up one line (but even with that.) Thank you anyway ! – Arnaud Chane Jan 07 '20 at 17:47
  • I've updated the answer to try to explain what the code does. – Booboo Jan 07 '20 at 21:17
0

Hey you can do this way ! print (" ?? - My name is {0} and I need to go in town " .format(input()))

Path- Or
  • 43
  • 1
  • 4
  • 13
  • 1
    Unfortunately this does not allow to prompt something before user input on the same line where the sentence will be written – Romain Jan 06 '20 at 16:35
  • 1
    Thanks for your answer but as Romain said, when I write "Arnaud", the code didn't prompt "??? - My name is " before my input. Plus the code doesn't store it in the variable "name". So in the end, the player will have to rewrite his/her name. =/ – Arnaud Chane Jan 07 '20 at 17:41
0

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.

tbrk
  • 156
  • 8