Print formated code in Jupyter - python

I have a code
"if x > 18:\n\tif y > 500000:\n\t\tprint('switch_credit_type')\n\telif y < 50000:\n\t\tif z > 2:\n\t\t\tprint('success')\n\t\telse:\n\t\t\tprint('get_more_info')\n\telse:\n\t\tprint('get_more_info')\nelse:\n\tprint('fail')\n"
Is any way to format it and print in jupyter cell like a
"if x > 18:
if y > 500000:
print('switch_credit_type')
elif y < 50000:
if z > 2:
print('success')
else:
print('get_more_info')
else:
print('get_more_info')
else:
print('fail')"

Related

Using “and” operator to compare two values

Very new to python and trying to figure out how to use the and operator to check if a number is between 50 and 100. Tried using and, && and || , but just getting invalid syntax python parser-16 error. If I take the and or alternative out of the code, then it partly works and dosn't give me a error message, though it dosn't check if the value is below 100, so presumly it must the and part that I'm doing wrong?
x = int(input("Enter a number between 0 and 100"))
if x < 50:
print("That is below 50!")
elif x > 50 and < 100:
print("That is above 50!")
else:
print("That number is too high!")
close!
x = int(input("Enter a number between 0 and 100"))
if x < 50:
print("That is below 50!")
elif x > 50 and x < 100:
print("That is above 50!")
else:
print("That number is too high!")
if x > 50 and x < 100 you have to reference it each time you check if its true
Alternative solution:
x = int(input("Enter a number between 0 and 100: "))# for a better look
if x < 50:
print("That is below 50!")
elif 100 >= x >= 50:# the numbers 50 and 100 shall be inclusive in one of the three params
print("That is between 50 and 100!")
else:
print("That number is too high!")
To simplify it more, you can write as below.
x = int(input("Enter a number between 0 and 100"))
if x < 50:
print("That is below 50!")
elif 50 < x < 100:
print("That is above 50!")
else:
print("That number is too high!")
If x<50:
print('Below 50')
elif x>50 and x<100:
print('Between 50 and 100');
else:
print('Above 100');
Try this

Using if with or

Is it possible to use IF with OR to check if one condition applies to 2 or more variables?
I need it like this, but when I put or between them, it doesn't work.
I've tried like this too:
elif x OR y >0: # (for Quadrant II)
My code:
x = int(input()
y = int(input())
if x < 0 and y > 0:
print("Quadrant I")
elif x > 0 OR y > 0:
print("Quadrant II")
else:
print("Quadrant III")
In the Python, 'OR' is to be 'or'
x = int(input())
y = int(input())
if x < 0 and y > 0:
print("Quadrant I")
elif x > 0 or y > 0:
print("Quadrant II")
else:
print("Quadrant III")
you are using OR you should use or.
Also you forgot to put indent in else statement.
I hope this will help
I´ve tried like this and it worked for me.
x = int(input())
y = int(input())
if x < 0 and y > 0:
print("Cuadrant I")
elif x or y > 0:
print("Cuadrant II")
else:
print("Quadrant III")

why is my try- except block stuck in the loop?

I am trying to catch an Index out of range error with the following try and except block.
def getStepList(r, h, d):
x = len(r)-1
y = len(h)-1
list = []
while True:
try:
if x == 0 and y == 0:
break
elif x >= 1 and y >= 1 and d[x][y] == d[x-1][y-1] and r[x-1] == h[y-1]:
x = x - 1
y = y - 1
elif y >= 1 and d[x][y] == d[x][y-1]+1:
#insertion
x = x
y = y - 1
list.append(h[y])
print('insertion')
elif x >= 1 and y >= 1 and d[x][y] == d[x-1][y-1]+1:
#substitution
x = x - 1
y = y - 1
list.append(r[x])
print('substitution')
else:
#deletion
x = x - 1
y = y
list.append(r[x])
print('deletion')
except IndexError:
print('index error')
return list[::-1]
but it gets stuck in a infinite loop. I want it to ignore it and proceed appending the next instances. (For reference its a piece of code that uses a metric of another function to determine which words were inserted, substituted or deleted at each operation).
I feel like I should know this, but in all honesty I am stuck.
Don't do a while True. Change your while condition to:
while(not (x == 0 and y == 0)):
Inside your exception also add a break
except IndexError:
print('index error')
break
You could also add some checks to see what the specific Index Error might be:
d_len = len(d)
r_len = len(r)
h_len = len(h)
d_x_of_y_len = len(d[x][y])
if(x > d_len):
print("x is too big")
if(y > d_x_of_y_len):
print("y is too big")

How to use logical operators on input function within an if statement in python 3.7? [duplicate]

This question already has an answer here:
Why does this not work as an array membership test? [duplicate]
(1 answer)
Closed 4 years ago.
I'm still in the early stages of learning python so this is probably an easy question that I just can't find the answer for. I'm trying to take user input to define 2 variables and compare them using > and < in an if statement.
line 6-11 in the code below I've also tried ...is False: and also y > x is True
print("what is x")
x = int(input('> ))
print("what is y")
y = int(input('> ))
if x > y is True:
print("x > y")
elif x > y is not True:
print("y > x")
else:
print("whatever")
If x > y then it says y > x.
If y > x, it prints the else condition.
You need brackets around your x/y comparison. I modified the code and I think it works as you intended now:
print("what is x")
x = int(input('> '))
print("what is y")
y = int(input('> '))
if (x > y) is True:
print("x > y")
elif (x > y) is not True:
print("y > x")
else:
print("whatever")
EDIT: As others have pointed out in the comments, you don't have to explicitly compare the result of x > y. You can just do this:
print("what is x")
x = int(input('> '))
print("what is y")
y = int(input('> '))
if x > y
print("x > y")
elif y > x:
print("y > x")
else:
print("whatever")

Convert from for-loop and while-loop to while-loop and while loop Python

for x in range(0,len(b)):
if x+1 < len(b):
if b[x][1] == 'B' and b[x+1][1] == 'B':
a.append([b[x][0], b[x][2]])
elif b[x][1] == 'B'and b[x+1][1] == 'I':
kata = b[x][0]
a = 1
while True:
if x+a < len(b):
if b[x+a][1] == 'I':
kata += ' ' + b[x+a][0]
a += 1
elif b[x+a][1] == 'B':
break
else:
break
a.append([kata, b[x][2]])
else:
if b[x][1] == 'B':
a.append([b[x][0], b[x][2]])
Can someone help me to convert the for-loop become while-loop? and the while-loop stay while-loop?
a for loop of the form
for x in y:
#code
can always be turned into a while loop of the form
i=0
while i < len(y):
x = y[i]
#code
i += 1
since x in your case is just iterating through values 0 to len(b) you can further reduce it down to:
x=0
while x < len(b):
#code
x += 1

Categories