-2

I am very new to programming and I've just been learning to write basic python using sublime text and running my code though the sublime python system build. I am trying to write a basic VAT calculator that will take user input and add the amount of 20% on the subtotal. Here is the code:

subtotal = float(input("Enter the #: "))
vat = float(1.2)
total = subtotal*vat
print(total)

However when I run this, the program spits nothing back. Would appreciate any help on this as I am a newbie.

Thank you

AMC
  • 2,642
  • 7
  • 13
  • 35
ghost
  • 13
  • 5
  • 3
    I just ran your code and it works for me! One quick note though, in line 2, there's no need to wrap a `float()` function as `1.2` is automatically treated as a `float` – ccl Feb 27 '20 at 18:54
  • I don't use sublime, but your code worked fine for me. I stuck it in a file called `vat.py` and ran `python3 vat.py` via terminal and it did what you expect. As @ccl pointed out, the `float(1.2)` is not necessary. – WGriffing Feb 27 '20 at 18:55
  • It works fine for me – Ananth.P Feb 27 '20 at 18:56
  • https://forum.sublimetext.com/t/build-broken-python-3-input/44988 – AMC Feb 27 '20 at 19:24
  • 1
    Does this answer your question? [Issue with Sublime Text 3's build system - can't get input from running program](https://stackoverflow.com/questions/19254765/issue-with-sublime-text-3s-build-system-cant-get-input-from-running-program) – AMC Feb 27 '20 at 19:26
  • Can you explain how you chose the accepted answer? – AMC Feb 27 '20 at 19:28
  • Thanks @B1CL0PS Running the code from the terminal worked perfectly fine. – ghost Feb 27 '20 at 19:36

2 Answers2

0

This code looks totally fine to me and running it behaved as I would expect:

$ python test.py
Enter the #: 10
12.0

Just guessing at a few ways it might not be working:

  1. You are typing a number into the terminal after you run it, right? Calling input will pause the program until you type some string and then press enter. Are you doing that?
  2. How are you running the code? You should run it from a terminal (1. open the terminal. 2. cd to your python file. 3. type python3 myfile.py.) If you saved this code in a python file and then ran it from your desktop environment (like by double clicking it in Windows) then it actually will work but the terminal window will close as soon as the program finishes so it will look like nothing happens. If you want this to work, just put input("Press Enter to exit...") as the final line of your program so it will wait and not close right away.

Also -- a few tips:

  1. On line 1, you might want to handle the case where the user enters something that isn't a float -- if they type in some letters instead the program will crash with an exception. You can easily do this with a try...except block.
  2. On line 2, float(...) is unnecessary because 1.2 is already a float.
  3. Actually, though, when working with money it's a good idea to not use floats because of floating point precision errors. Instead, do your calculations as integer numbers of cents and then divide by 100 at the very end.
QuinnFreedman
  • 2,242
  • 2
  • 25
  • 42
  • _How are you running the code?..._ From the OP: _using sublime text and running my code though the sublime python system build_ – AMC Feb 27 '20 at 19:29
0
print("-" * 30)
print("Total amount: +VAT  £{:.2f}".format(round(total_amt*1.20, 2))) #shows receipt and VAT while also being rounded to 2 decimal places
print("-" * 30)
Kalana
  • 5,631
  • 7
  • 30
  • 51
alfie
  • 1