Is it possible to input in the middle of a Python 3 print?

Asked

Viewed 686 times

-1

Something like a input in the middle of a print, it is possible?

I’m trying to do something like this:

print (f "Eu quero digitar {input ('')} palavras.")

If I need some pygame module or whatever...

What I want is an input type response between two texts like:

QUERO DIGITAR |_ PALAVRAS

| means the mouse cursor indicating where to type and _ means the input.

I’m using Python 3.


Thanks already to everyone for the possible help. I think the next versions of python should implement this. pq it is a good thing I ask the user for ex n° of _______ houses

_______ would be where the user would type this into an input. It would be a kind of print(f"I have {input()} ideas")

  • 3

    Please write your question in Portuguese since we are on [pt.so].

2 answers

3

Man, from what I understand it’s super simple.

It would be something like:

input = raw_input()
print("I want to type " + input + " words.")

And if you want to write the first part of the text, stop the execution, read the input and continue the text you can use:

print("I want to type ", end="")
input = raw_input()
print(input + " words.", end="")

But this latest implementation will only work in Python 3. As you mentioned in the title that you are using Python 3, so no problem :)

This last parameter allows you to choose which will be the last character, which by default is "\n", but this way I showed you define for an empty string, IE, nothing.

  • raw_input was not renamed to input() ? raw_input does not work here... I tried with input and did not get to my result. By the way I did a search and it was really renamed to input() only, ie raw_input no longer works, it is even unrecognizable in pycharm

  • I didn’t get what I want yet, the raw_input() Function doesn’t work in python3.7

  • Sorry for this error. I did it in my head without testing. But the important thing is to pass the idea hehe ^^

  • But I think Pedro Heriqu’s comment is more in line with what you’re looking for...

1

  • I think this is what you want:

python

# Definimos o começo e o fim da frase:
começo_da_frase = "Eu quero digitar "
fim_da_frase = "____ palavras"
espaço = len(começo_da_frase)*" "

print(espaço,fim_da_frase,"\r",começo_da_frase,flush=True,end="")
input('') #explicação abaixo
  • Explanation: This is one of the ways to solve, not very elegant but it was what I was able to think fast here.

So here’s the thing, you want to print:

"I want to type |___ words" and receive an entry that will be inserted in the location marked with "|".

  • Soon you need to get back in line.

Then you print one space for the beginning of the sentence, print the final sentence, back to the beginning of the line " r" and print the beginning. Now you stopped the cursor at the very beginning of "___", then you just ask for the input!

But attention! When giving print, the function throws you at the beginning of the next line, avoid this by putting the clause end = ""

Defects in the solution:

  • By typing more characters than Len("____") user text starts to delete the final sentence

Remarks:

  • Perhaps it is possible to solve the problem I mentioned with the package curses, he manages the prompt giving you more control over these things, but it’s too advanced for me haha

  • The name of " r" is Carriage Return, or CR

  • Unfortunately it has not yet reached the result I want (but I am grateful for the help/willingness) anyway, I even imagined that this flush would help me, I wanted my input not paralyze the next print...

  • So working with Standard Library alone will give you a bit of a headache. Curses should solve your problem, but I have no idea how to run the code, although this looks pretty basic I haven’t found any elegant way to solve.

  • And you should think about migrating your code to something with a more modern user interface, Kivy is a great library to start, especially because it allows you to export to mobile without having to program hell in Java.

  • I appreciate the tips. I’ll give a search anyway. I ask you to look at the final comment of the topic

Browser other questions tagged

You are not signed in. Login or sign up in order to post.