r/pythonhelp • u/janetliy2001 • Sep 09 '21
HOMEWORK Python homework help: I just started learning python so this is prob very very easy for you guys. But i’m very stuck on this problem. I cannot figure how to make it subtract. It works fine for addition but it always get stuck when it gets to the subtraction part. Thank you so much
3
u/skellious Sep 09 '21
just to point out, it's not working for addition either (or at least, not in the way we'd expect it to work). as /u/LingStuffs says, you're currently "adding" two strings, which means smushing one into the other. you want to be adding them as numbers.
1
u/janetliy2001 Sep 09 '21
Oof yes you’re right. My head was too wrapped over the subtraction part that i didnt realize
1
u/Tomboyish44 Sep 09 '21 edited Sep 09 '21
Heyo
By default, the input() function takes in* the input given by the user as a string type value (str). Even if you enter an integer as the input, it is stored as a string-type value if input() isn't explicitly type-casted to be another data-type such as int() or float()
Say the input given by the user is 30, it is stored as "30". It's stored as a string-type value, not as an integer-type value since input()'s data-type is str()
Since the input is a string, we can only perform operations that can be done on strings, and subtraction is not an arithmetic operation that can be performed on string-type values
String concatenation (concatenating/combining two strings) is an operation that can be performed on strings, that's why the code containing A + B runs without terminating the program. [However, the output is mathematically incorrect since the code isn't adding two numbers, just combining two strings of text. The expected output for the addition part is 53]
Since subtracting two strings isn't a operation that can be performed on strings, the program terminates when it encounters that line of code
You'll have to typecast both the input() functions to float() -
A = float(input("Enter the first number: "))
B = float(input("Enter the second number: "))
addition = A + B
subtraction = A - B
print(addition)
print(subtraction)[Chose float() over int() so that accurate results would be displayed in case the user inputs decimal values]
TL;DR - input() needs to be explicitly type-converted into int() or float() to perform mathematical operations on A and B. input() takes in input as strings by default if not type-casted.
2
u/janetliy2001 Sep 09 '21
Wow thank you so much for the detailed explanation. My teacher definitely didnt teach us all that in such details but I totally understand the concept now. And using your advice i added the float to my code and it successfully worked! Thank you so much again
1


7
u/LingStuffs Sep 09 '21
Because A and B are both inputs they are strings. So whilst A+B works, what it's actually doing is concatenating the strings A and B. So if A="1" and B="2", A+B actually equals "12". You need to wrap the input() in an int() datatype which means Python will process the values A and B as numbers rather than characters. So A=int(input('enter first number')) and then do the same for B :)