How to change variable value in python with if condition [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 2 years ago.
Improve this question
How can I change python variable value with if.
so like we have a=5, and there we have an input b, if b =5, i want the program to change a to 3.
This is the program I tried to make but it didnt work, hope you have solution.
a=5
b=int(input())
if b==5:
a==3
print(a)

b = int(input())
if b == 5:
a = 3
print(a)
If you want to set a new variable only use one equal sign not two.

you need to change the 4th line from a==3 to a=3when you use double '=' you are comparing and the result is boolean (true or false) and when you use just a single '=' your are assigning a value to something.
For example:
a = 3==4 print(a)
This will output FALSE, since 3 doesn't equal 4 and that value was assigned to variable a

Related

Indexing a list of strings in for 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 12 months ago.
Improve this question
For this code:
s = ['apple', 'banana', 'cherry']
for v in s:
print(v[1])
Why does printing this index result in this output?
p
a
h
I know that it is outputting the 2nd position within each string, but I was expecting it to output banana since it was the in the 2nd position in the list. Is there a way to get the output to be banana?
As Bill says, the for loop prints the second character of each string within your list s. I think you may be misunderstanding how the for loop works.
for v in s iterates over the elements of s, setting v=apple, then v=banana, and finally v=cherry. When v=apple, you are printing v[1] which is the second character of apple as strings are iterable in python - this is "p". When v=banana, again you are printing the second character of v which is now "a".
To get the second element of any list, you simply do list[1] - which is just print(s[1]) in this case.

Why is "if[ i %2==0]" always true? [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
Need support/please clear my doubt:
def test(strValue):
for i in range(len(strValue)):
if[ i %2==0]:
strValue.replace(strValue[i] ,strValue[i].upper())
print('if loop '+strValue)
else:
strValue.replace(strValue[i] ,strValue[i].lower())
print('else loop '+strValue)
**input:**
test('apple')
**output:**
if loop apple
if loop apple
if loop apple
if loop apple
if loop apple
1.why if[ i %2==0] is always true
2.why strValue is not changed
1- [i%2==0] is a list of one element being the result of your test. In python, the brackets build a list. So you should just remove the bracket and your test will be fine.
2- strValue.replace(strValue[i] ,strValue[i].lower()) is not in-place modification. Strings are immutable in python so you need to store the result of this call in strValue and hence erase the former value.
So the working code in your case is:
def test(strValue):
for i in range(len(strValue)):
if i %2==0:
strValue = strValue.replace(strValue[i] ,strValue[i].upper())
print('if loop '+strValue)
else:
strValue = strValue.replace(strValue[i] ,strValue[i].lower())
print('else loop '+strValue)
To answer your problems
The value of [i%2==0] is not always equal to True, it is either [True] or [False] depending on the i value but the non-empty list like [True] or [False] is always True so try to do this way
i%2==0 instead of [i%2==0]
You're replacing the strValue but not assigning it back. So do this way-
strValue = strValue.replace(strValue[i] ,strValue[i].upper())
DEMO & EXPLANATION: https://rextester.com/AHJHS98230

Why I can't get an index from list filled with Ints? [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
So, it gives me
AttributeError: 'int' object has no attribute 'index'
length = int(input())
arrayOfStrings = input()
number= 0
arrayofnumbers = list(map(int, arrayOfStrings.split()))
minimum = arrayofnumbers[0]
for i in range(1, len(arrayofnumbers)):
if minimum<arrayofnumbers[i]:
number = arrayofnumbers[i].index(i) ErrorString
minimum = arrayofnumbers[i]
elif minimum==arrayofnumbers[i]&number<i :
number = i-1
i+=1
print(number)
There are some errors:
elif minimum==arrayofnumbers[i]&number<i you have to do an AND logic here, so use and instead of &.
arrayofnumbers[i].index(i) arrayofnumbers[i] is an integer, so you have to cast into a str before use "index" method
Why i+=1? For loop increment i by default in range
Use built-in min() method 'cause it's better if you want to find the minimum element of an array:
min(arrayofnumbers)
arrayofnumbers[i] is an integer, not a string or object. So, you can not index into integer value. instead you can modify error line to number = arrayofnumbers.index(i), because list supports the indexing.

Dynamic programming: Number of ways of partitioning a set of numbers [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 years ago.
Improve this question
While reading an algorithm book, I found the following exercise.
Given a set of n elements, write an algorithm that finds number of
ways of partitioning it.
Example: When n = 2, there are 2 ways of partitioning the set(into
two sets with one element, or into the original set and the empty set).
And instead of the algorithm, I tried the python code using dynamic programming.
def ways(n):
dp = [0]*(n+1),
sum = [0]*(n+1) ## declaring 2 arrays of n+1 size
dp[0] = 0
dp[1] = 1
sum[0] = 0
sum[1] = 1
lastcalc = 1 # last calculated var
for i in range (2,n):
if lastcalc < i/2 :
for j in range (lastcalc, i/2):
sum[j] = sum[j-1] + dp[j]
lastcalc = (i/2) # update the lastcalculated variable
dp[i] = sum[i/2]
return dp[n]
print(ways(2))
But, the code won't work and gave me an error.
TypeError: 'tuple' object does not support item assignment
My question: how can I fix this? Can I say this code applied a dynamic programming?
You have a comma at the end of the declaration of dp. This makes it a tuple, not a list, and tuple are not modifiable. Just remove it, it's a typo.

Why would the union of an empty set with another set results in an empty set 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 6 years ago.
Improve this question
a = set()
b = set([1,2])
print a.union(b)
The result is an empty set. But if a is not an empty set, the result is correct.
No! The result of the union between the set empty and b is always b.
a = set()
b = set([1,2])
print a.union(b) #return {1, 2}
The intersection between an empty set and anything is the set empty.
print a.intersection(b) #return set()

Categories

Resources