Python: How do I get user input in the middle of a sentence (fill-in-the-blank style) using the input function?

[亡魂溺海] 提交于 2019-11-29 16:41:38

you can do something like this

x=input("What is the sales tax in your area?    % \x1B[5D")

it first print the line and then the escape sequence \x1B[5D moves the cursor 5 place backwards. But what the 1st answer says, you can not do this without knowing beforehand how long the input is going to be. If it is longer then it will overwrite the percentage sign. you can check here for more such escape sequence

Also if you don't want this very much urgently then I would suggest you to ask the user to input in percentage form instead of putting a % sign at the end of inpur

I have a solution for you. Just a trick, didn't find direct solution even after searching for long. But one thing is for sure, you need to know the length of input, else for very long input the ! will be overwritten. Code below:

x = float(input("What is the cost of your meal? "))
y = input("What is the sales tax in your area?  %\rWhat is the sales tax in your area?")

y = float(y)/100
cost = (x+(x*y))

print("The cost of your food is ", cost)

You can't do that with input() without knowing how long the input will be. You could just ask the user to answer in percent. You could use ANSI escape codes to put the percent sign after the place where you get input, but it will be overwritten if you type until you reach the percent sign. If you really need that, then you could use curses for the whole thing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!