1

I am trying to create a program that calculates sales tax. I first ask the user what was the total cost of their meal. I then ask them what is the sales tax of their area in the form of:

y = input('What is the sales tax of your area? ')

I want to have a percent sign "%" at the end of the question so that the users sees the question as:

What is the sales tax of your area?_% , where the user can enter a number between the question and the percent sign (denoted by the underline).

This is what I have tried:

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

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

print("The cost of your food is " + cost)
55 Cancri
  • 1,075
  • 11
  • 23

4 Answers4

1

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

Eular
  • 1,707
  • 4
  • 26
  • 50
  • The issue with your final suggestion is that the user's input can no longer be converted into a float because the "%" sign is not an integer... Unless you know of a function that can remove the final character of a user's input? So if they enter "6%" as their answer, the percentage symbol can be taken off and then the code I have above can continue to function properly. – 55 Cancri Jul 07 '16 at 14:03
  • no, i mwan ask like this `y = input("What is the sales tax in your area? (in percentage)")` and if you want to do what you said put `y=y[:-1]` – Eular Jul 07 '16 at 14:08
  • Oh you mean in decimal format? I suppose that is one solution, but I wanted the program to be as easy as possible for the end user and I am not convinced that Python is unable to produce the results that I have requested. – 55 Cancri Jul 07 '16 at 14:12
  • so the solution with the escape sequence, does it do your job? – Eular Jul 07 '16 at 14:13
  • It did not. I was prompted for user input after "[5D". Is there a certain library I need to import to use escape sequences? I also did not see that escape sequence on the link you provided. – 55 Cancri Jul 07 '16 at 14:16
  • what do you mean you prompted for user input after [5D? that is a escape sequence and will not be printed at all – Eular Jul 07 '16 at 14:19
  • The way you have it in your answer, "% \x1B[5D" is printed after the question, then it allows me to give an answer. – 55 Cancri Jul 07 '16 at 14:21
  • I don't understand, its working properly for my system. Are you working in windows 8.1 or less? – Eular Jul 07 '16 at 14:44
  • No I am using Mac with Idle 3 – 55 Cancri Jul 07 '16 at 14:46
  • well, I don't know about mac because i don't have one, but it seems what you are using, doesn't understand the ANSI escape sequences and also Idle does not support escape codes. Try using it in terminal with proper escape codes for os x terminal. – Eular Jul 07 '16 at 15:01
1

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)
Deca
  • 1,155
  • 1
  • 10
  • 19
  • Unfortunately your solution produces this result: "What is the sales tax in your area? % What is the sales tax in your area?", where user input is prompted after the second time the question is asked. – 55 Cancri Jul 07 '16 at 14:29
  • ahhh...u might be using some editor....in editor it wont work...u can try in any notepad and run from command line, coz in editor such as eclise, the cursor even blink many times at rite place! I believe some eclipse take \r as new line as well – Deca Jul 07 '16 at 14:31
  • it seems dere is error with few editors related to special charactrs like carriage return and all.... Eclipse:-- http://stackoverflow.com/questions/14792478/how-to-get-carriage-return-without-line-feed-effect-in-eclipse-console IDLE:- http://stackoverflow.com/questions/35768165/carriage-return-not-working-in-idle – Deca Jul 07 '16 at 14:39
0

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.

randomdude999
  • 701
  • 7
  • 20
0

I recently had the same problem and I discovered a way to display what you are looking for! If you import getpass, then you should be able to use the code below to get the result you want.

import getpass

salesTax = getpass.getpass("The sales tax in my area is " + getpass.getpass() + " percent."

If you run this, then the first thing displayed should be "Password:" If you enter 5, for example, then the output will be "The sales tax in my area is 5 percent." I am not the best at explaining things, so if you want to see it work in action, I made a repl that does this. https://repl.it/@ERokos_tcsyl/tester

  • Nice try. "Where the user can enter a number between the question and the percent sign". I tested your link. No luck. – qräbnö Feb 19 '20 at 22:50