Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic subtraction error
#5
Firts, your original code - no need to wrap input() function in str(). input() returns str in any case.

As suggested by jefsummers - you can use Decimal, instead of float
from decimal import Decimal
var1=Decimal(input("What is Var1?"))
#For example user inputs 8.10
var2=Decimal(input("What is Var2?"))
#For example user inputs 6.80
var3 = var1 - var2
print(var3)
Another option is to use string formatting when printing the result of deduction
var1=float(input("What is Var1?"))
#For example user inputs 8.10
var2=float(input("What is Var2?"))
#For example user inputs 6.80
var3 = var1 - var2
print(f'{var3:.2f}')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Basic subtraction error - by rix - Oct-10-2019, 08:01 PM
RE: Basic subtraction error - by buran - Oct-10-2019, 08:09 PM
RE: Basic subtraction error - by rix - Oct-10-2019, 08:51 PM
RE: Basic subtraction error - by jefsummers - Oct-10-2019, 10:43 PM
RE: Basic subtraction error - by buran - Oct-11-2019, 06:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on basic Post API, FastAPI marlonbown 1 3,044 Nov-10-2022, 10:02 AM
Last Post: Larz60+
  forloop to compute sum by alternating from addition to subtraction JulianZ 3 3,519 Apr-02-2022, 09:36 AM
Last Post: DeaD_EyE
  List index out of range error when attempting to make a basic shift code djwilson0495 4 4,448 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Odd numpy error with subtraction DreamingInsanity 5 4,104 Jun-01-2020, 02:49 PM
Last Post: DreamingInsanity
  How to make a subtraction within a range of numbers? Alberto 3 12,074 May-08-2017, 09:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020