Switch the integer to its opposite 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 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

Related

Variables Not subtracting 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 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)

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.

Python handling floating point with multiply float [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 2 years ago.
Improve this question
I encountered a floating point problem when dealing with flow:
a = "0.0003"
b = pow(10, 4)
c = int(float(a) * b)) // int so i don't want the decimal places behind
i got the following result 2.9999999999999996 which is wrong
I also tried decimal:
c = int(Decimal(a) * Decimal(pow(10, 4)))
but still got the wrong result 2.999999999999999737189393390
Expected result is 3
In Javascript we can use bignumber.js
Is there a way i can avoid this kind of problem in python? maybe some library like in javascript
Thanks
Make sure a is the string, not the float. Then it should work.
int(Decimal('0.0003') * pow(10, 4))
# returns 3

Using a break statement to exit a loop [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 2 years ago.
Improve this question
i want to detect when the program sees the value 26, i have a function that looks like this
val = 26
for n in range (0,101):
if n in val:
print(n,"is the value i am looking for")
break
else:
print(n)
I keep getting this error:
TypeError: argument of type 'int' is not iterable
What is wrong with this function
n in val checks whether the value n is in the iterable val. The correct check here is n == val

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.

Categories