Variables Not subtracting in python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
does anyone know why I can't subtract a variable from another variable if both are defined as numbers?
A = 100
B = int(input("Amount? "))
A - B
print(A)
This is a very simplified version of what it actually is but still the same concept, I am quite new to coding so sorry if this is really dumb of me to ask. Thanks :)

Replace A - B to A = A - B or use the shorthand notation A -= B which is the same as the first alternative.

You are never assigning the value to A.
Here is the corrected code:
A = 100
B = int(input("Amount? "))
A = A - B #Issue was here
print(A)

Related

Python code for string equal not working for python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
i can not get My python code to work it does not replay after i give a answer
Weight = input("Weight: ")
lbsorkg = input("(L)bs or (K)g: ")
if lbsorkg.upper == "K":
FinalPounds = float(Weight) * 2.205
print(f"You are {FinalPounds} pounds")
And I get this
Weight: 12
(L)bs or (K)g: k
Please help me slove this issue
the problem is with the strings' attribute upper, which is not a simple attribute but an instance function.
You must call it, like this:
>>> "k".upper()
'K'
In your case, you should call lbsorkg.upper(), like this:
if lbsorkg.upper() == 'K':
...
I don't understand what's wrong with this.

Why am I getting two diferent outputs with my python code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def kaka(name):
r=''
for ch in name:
r=r+ch*3
return r
Output:
>>> kaka('Mississippi')
>>> 'MMMiiissssssiiissssssiiippppppiii'
But for this code:
def kaka(name):
for ch in name:
r=''
r=r+ch*3
return r
I am getting output as: iii
That's because in your second code you're re-assigning r back to the empty string ''. Thus you only get the final character multiplied 3 times (which for Mississippi is i).
You are getting 2 different outputs because in the first code you are initialising the value of r i.e r = '' outside the for loop and in the second program you are initialising value of r inside the for loop.

Python appending empty list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am very new to Python and trying to learn by trial-and-error, so my question may sound naive for the community.
Let's say I have two empty lists with only the first element defined:
a = [[]]*20
a[0] = 0
b = [[]]*20
b[0] = 1
I want to use a for loop for creating the other elements of the lists:
x = 20
for i in range(1,x):
a[i] = b[i-1],
b[i] = a[i-1]+b[i-1]
What I obtain is the following error:TypeError: can only concatenate tuple (not "int") to tuple.
Basically I am trying to reproduce the fibonacci series (a famous starting point in Python tutorial), but I would like to experiment other ways of obtaining the same output.
Thank you!
The problem is on this line:
a[i] = b[i-1],
Notice the comma at the end? That makes python think you're dealing in tuples. Remove it and the error will be gone.

Switch the integer to its opposite in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm wondering if there is some built in function in python that will change whatever the sign of an integer is to its opposite.
example of what I'm looking for:
num = 1
num2 = -2
# where flip() is a hypothetical function
flip(num)
flip(num2)
print(num)
print(num2)
>>> -1
>>> 2
I know how to create my own function to do this, but it seems there may be a built-in, or some short hand way to go about it.
So this was actually way more simple than I was trying to make it.
num1 = 1
num1 = -num1
print(num1)
>>> -1
num1 = -num1
print(num1)
>>> 1
Two negatives equal a positive

Python: How to store string inputs on a array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I would like to store strings on a array. This strings are produced in this loop:
while (count < ts ):
dt=tb
t1=count+180
t2=t1+360
dt1=dt+t1
dt2=dt+t2
slice=stream.slice(dt1, dt2)
B=str(dt1)
E=str(dt2)
slice.write(station+'_'+comp[i]+'_'+B+'_'+E, format="MSEED")
count = count + 360
bb=[]
name=station+B+'_'+E
a=[str(name)]
bb.append(a)
But it doesn't work. The variable name is from type:
name=2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z
And I would like to have an array like that:
bb=[2011-05-22T23:42:00.000000Z_2011-05-22T23:48:00.000000Z, 2011-05-22T23:48:00.000000Z_2011-05-22T23:54:00.000000Z, 2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
But what bb returns me is an array with the last element called:
bb=[2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
If I do it manually:
bb.append('2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z')
It works perfectly because I put the ''. But I need to it in a automatic way.
Any suggestion?
Thanks in advance!
Declare bb outside the loop and a will be a list. You will get a list of lists(not in the way you asked for)

Categories