Example:
>>> x = 10
>>> x = x > 9 and 0 or x
>>> x
10
while:
>>> x = 10
>>> x = x > 9 and 1 or x
>>> x
1
Obviously 0 is treated as boolean False. How to use inline comparison to assign integer 0?
You probably want this
x= 0 if x > 9 else x
This is the python way of traditional ternary operators in c like languages.
The code snippet that you have will always give x or 1 for x > 9:
x = x > 9 and 0 or x
This is interpreted as
x = (x > 9 and 0) or x
x = 0 or x
x = x
The other version
x = x > 9 and 1 or x
is interpreted as
x = (x > 9 and 1) or x
x = 1 or x # or gets short-circuited so second expression is not evaluated
x = 1
try this:
x = 0 if x > 9 else x
Related
Hey guys I hope you're doing well.
The problem I have is that my loop is not well defined, therefore the condition that I give it is not met. The print of the DataFrame that I implemented inside the while loop is not performed when the condition is not met.
This is the code I have so far. By implementing the while loop it stopped returning me the modified dataframe. As I said before, the loop is poorly constructed.
Dataframe content:
1 2 3 4 5 6 7 8 9
A 5 3 X X 7 X X X X
B 6 X X 1 9 5 X X X
C X 9 8 X X X X 6 X
D 8 X X X 6 X X X 3
E 4 X X 8 X 3 X X 1
F 7 X X X 2 X X X 6
G X 6 X X X X 2 8 X
H X X X 4 1 9 X X 5
I X X X X 8 X X 7 9
Code:
import pandas as pd
def modifyDF():
T = pd.read_fwf('file', header= None, names=['1','2','3','4','5','6','7','8','9'])
T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})
df = pd.DataFrame(T)
print(T,'\n')
x= input('row: ')
y= input('column: ')
v= input('value: ')
while 'X' in df:
f = df.loc[x,y]= v
print(f)
while 'X' not in df:
break
modifyDF()
Expected OUTPUT:
1 2 3 4 5 6 7 8 9
A 5 3 X X 7 X X X X
B 6 X X 1 9 5 X X X
C X 9 8 X X X X 6 X
D 8 X X X 6 X X X 3
E 4 X X 8 X 3 X X 1
F 7 X X X 2 X X X 6
G X 6 X X X X 2 8 X
H X X X 4 1 9 X X 5
I X X X X 8 X X 7 9
row: D #For example
column: 2 #For example
value: 1 #For example
#The modified dataframe:
1 2 3 4 5 6 7 8 9
A 5 3 X X 7 X X X X
B 6 X X 1 9 5 X X X
C X 9 8 X X X X 6 X
D 8 1 X X 6 X X X 3
E 4 X X 8 X 3 X X 1
F 7 X X X 2 X X X 6
G X 6 X X X X 2 8 X
H X X X 4 1 9 X X 5
I X X X X 8 X X 7 9
#The goal would be for this to run like a loop until there are no 'X' left in the dataframe.
I really appreciate your help :)
Generally speaking, you'd better not loop through a pandas DataFrame, but use more pythonic methods. In this case, you need to move your while loop a bit higher in your code, before the input statements, so your function would become:
def modifyDF():
T = pd.read_fwf('file', header=None, names=['1','2','3','4','5','6','7','8','9'])
T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})
df = pd.DataFrame(T)
print(T,'\n')
while df.isin(['X']).any().any():
x = input('row: ')
y = input('column: ')
v = input('value: ')
df.loc[x,y] = v
f = v
print(f)
Also remember that f = df.loc[x,y]= v is wrong in Python.
I'm trying to execute code like the following
y = 6
x = 7 if y/6 == 1 elif y/6 == 2 x = 5 else x = 4
Simply, it does not work. I'm not sure how to make elif statements fit into defining variables on one line.
I'm able to use else statements like the following
y = 6
x = 7 if y/6 == 1 else x = 4
I want to be able to set x to 7 if y/6 ==1, x to 5 if y/6 == 2 and x to 4 if neither of those are the case
Use chained ternary expressions:
>>> y = 6
>>> x = 7 if y/6 == 1 else 5 if y/6 == 2 else 4
>>> x
7
The way to read this is:
x = ((7) if y/6 == 1 else ((5) if y/6 == 2 else (4)))
i.e. each expr1 if pred else expr2 is itself an expression.
You only need x = ... once at the beginning. The rest of the expression is evaluated as a value that will be assigned to x. You don't need to mention x again.
x = 7 if y/6 == 1 else 5 if y/6 == 2 else 4
In general, the syntax is
var = value1 if condition1 else value2 if condition2 else value3
Resolve CODE 2 to print output as CODE 1 and give the reason why both of the codes have different outputs.
Fibonacci Series
CODE 1
x = 0
y = 1
while x < 10:
print(x)
x, y = y, x + y
output
0
1
1
2
3
5
8
CODE 2
x = 0
y = 1
while x < 10:
print(x)
x = y
y = x + y
Output
0
1
2
4
8
Those are simply not identical.
In the first code block y becomes x+y and in the second code block y becomes 2*y.
Just a quick note the output of the second code block is 0 1 2 4 8 and not what you wrote (this was fixed).
The python code, given in a lab for a class I'm in when executed through pycharm, ouputs 0. However, upon looking at the code, it should be 1. Why is it 0?
X = 4
C = 0
while X > 0:
if X % 2 == 0:
C = C + 1
else:
C = C - 1
X = X - 1
print(C)
The code is fine. Your X will go from 4 to 1 and on X = 0, the program will leave the loop. Printing X-C for each iteration gives the values:
X-C
4-1
3-0
2-1
1-0
If you want it to go till 0, make the condition as:
while X >= 0:
The loop executes 4 times: When X = 4, C = 1; when X = 3, C = 0; when X = 2, C = 1; when X = 1, C = 0.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the value of y after the following statements?
x = 100
y = 0
while x > 50:
y = y + 1
x = x - 1
I'm having trouble with questions that involve 2 variables.
Step through the first few iterations of the loop, look for a pattern, and extrapolate.
x = 100 # x = 100
y = 0 # x = 100 y = 0
if x > 50: # x = 100 y = 0
y = y + 1 # x = 100 y = 1
x = x - 1 # x = 99 y = 1
if x > 50: # x = 99 y = 1
y = y + 1 # x = 99 y = 2
x = x - 1 # x = 98 y = 2
if x > 50: # x = 98 y = 2
y = y + 1 # x = 98 y = 3
x = x - 1 # x = 97 y = 3
if x > 50: # x = 97 y = 3
y = y + 1 # x = 97 y = 4
x = x - 1 # x = 96 y = 4
if x > 50: # x = 96 y = 4
y = y + 1 # x = 96 y = 5
x = x - 1 # x = 95 y = 5
if x > 50: # x = 95 y = 5
y = y + 1 # x = 95 y = 6
x = x - 1 # x = 94 y = 6
...
if x > 50: # x = 52 y = 48
y = y + 1 # x = 52 y = 49
x = x - 1 # x = 51 y = 49
if x > 50: # x = 51 y = 49
y = y + 1 # x = 51 y = 50
x = x - 1 # x = 50 y = 50
if x > 50: # x = 50 y = 50
(false, end process)