-3

The code works as expected in the python shell, launched through idle. When running the file via sublime text, it only prints "please enter your price:" to the console without running the remainder of the code after submitting my input. Hitting enter on the console only creates a new line. When running the code in the python shell, the rest of the code executes after submitting the int into the tip calculator.

def calculateit():
     price = input("please enter your price: ")
     tip = int(price) * 0.25
     final = int(tip) + int(price)
     print ("since the price of your meal is " + str(price) + " your tip is " + str(tip))
     print ("the total cost of your meal is " + str(final))

calculateit()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Robert N
  • 21
  • 2
  • 1
    Sublime doesn't take user input last time I checked – OneCricketeer May 12 '18 at 15:07
  • 1
    Please try searching for the problem before posting. https://forum.sublimetext.com/t/is-this-a-bug-python-input-not-working-in-build-or-console/25719 – OneCricketeer May 12 '18 at 15:11
  • 3
    Possible duplicate of [Sublime Text 2 console input](https://stackoverflow.com/questions/10604409/sublime-text-2-console-input) – ash May 12 '18 at 15:20

1 Answers1

0

When you execute code in Sublime there is no way to capture input. More technically, the console is connected to stderr and stdout but not stdin. So it's not possible to run an interactive program from within Sublime directly.

However after seeing your code, a question arises - why don't you cast your price variable at the first time with user input method, so that you won't need multiple time to cast it from str to int and vice versa. To make you clear of this, see the following code , it's exactly the same but more smart approach i think.

def calculateit():
    price = int(input("please enter your price: "))
    tip = price * 0.25
    final = tip + price
    print (f"since the price of your meal is {price} your tip is {tip}" )
    print (f"the total cost of your meal is {final}" )

calculateit()
Innat
  • 16,113
  • 6
  • 53
  • 101
  • Okay, thank you. I will just need to run it in idle instead. The answer to your question is mostly just because I didn't know better. I was thinking of it line by line. – Robert N May 12 '18 at 17:21
  • 2
    @RobertN please, mark the answer if its helpful to you. Thanks – Innat Jun 17 '18 at 20:02