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")
Related
So I am learning python and there is something that bothers me. For the chunk of sample code below:
print("Test")
while True:
print("statement0")
x = input()
print(x)
if x == 1:
print("statement1")
y += input()
print(y)
elif x == 2:
print("statement2")
y += input()
print(y)
elif x == 3:
print("statement3")
y += input()
print(y)
elif x == 4:
print("statement4")
break
The if statements don't seem to be able to execute. After the value of x is printed it will loop back to statement0.
You have to first specify what will be the data type of the variable x.
Now as you are equating x to integers, you need to add int before the input() function.
Also, you have not initialized the variable y before, so python will throw an error. Define y beforehand with any integer value.
And try to keep the expressions inside parenthesis as it helps in easier understanding.
So, your final code becomes:
print("Test")
while True:
print("statement0")
x = int(input())
print(x)
y = 0 # initialize y with any value
if (x == 1): # edit
print("statement1")
y += int(input())
print(y)
elif (x == 2): # edit
print("statement2")
y += int(input())
print(y)
elif (x == 3): # edit
print("statement3")
y += int(input())
print(y)
elif (x == 4): # edit
print("statement4")
break
While taking the input, use x = int(input())
And, also y is not defined in this case, before doing something like y+=int(input()), you have to declare y above like y = 0
Thank you!
def multiplication_maker(m=1):
n = 1
while n < 11:
print(m,"*",n,"=",m*n)
n += 1
x = input("Enter the multiplication you want =" )
x = int(x)
multiplication_maker(x)
You could check the length by using len() then assigning 1 to it
def multiplication_maker(m=1):
n = 1
while n < 11:
print(m,"*",n,"=",m*n)
n += 1
x = input("Enter the multiplication you want =" )
if len(x) == 0:
x = 1
x = int(x)
multiplication_maker(x)
Solution
You can use the fact that an empty string is evaluated as false and set a condition
x = input("Enter the multiplication you want =")
if not x:
x = "1"
x = int(x)
multiplication_maker(x)
Smart
Using the fact that is a the left operand is false (ie empty string) then the right operand will be evaluated and returned you can do that trick
x = int(input("Enter the multiplication you want =") or "1")
multiplication_maker(x)
def multiplication_maker(m=None):
try:
m = int(m)
except:
m = 1
n = 1
while n < 11:
print(m, "*", n, "=", m * n)
n += 1
x = input("Enter the multiplication you want =")
multiplication_maker(x)
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")
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')"
Is there an equivalent of += for a string?
ie:
x = 1
while x <= 100:
y = x
if x % 3 == 0:
y = 'Fizz'
if x % 5 == 0:
y += 'Buzz'
if x % 7 == 0:
y += 'Foo'
if x % 11 == 0:
y += 'Bar'
print y
x += 1
raw_input('Press enter to exit...')
This should return a string and a second string if the same rules as with numbers applied. Is it possible to do this? Because just doing that returns TypeError: unsupported operand type(s) for +=: 'int' and 'str', even though y is a string to begin with, not an int.
If you do this:
You are concatenating a string to string:
x = 'a string'
x += '6'
print x
If you do this:
You concatenate int to string so you get error:
x = 'a string'
x += 6
print x
error:
TypeError: cannot concatenate 'str' and 'int' objects
You have to make sure variable type before doing '+' operation; based on variable type, python can add or concatenate
The following code works for me for Python 2.7.4 and Python 3.0:
a='aaa'
a+='bbb'
print(a)
aaabbb
That would be s1 += s2:
>>> s1 = "a string"
>>> s1 += " and a second string"
>>> s1
'a string and a second string'
>>>
Unlike Perl, Python mostly refuses to perform implicit conversions (the numeric types being the principal exception). To concatenate the string representation of an integer i to a string s, you would have to write
s += str(i)
I don't know, but maybe you are looking for operator
operator.iadd(a, b)¶
operator.__iadd__(a, b)
a = iadd(a, b) is equivalent to a += b.
http://docs.python.org/2/library/operator.html
x = 'a string'
x += ' and a second string'
print x
operator.iadd(x, ' and a third string')
print x
How can I concatenate a string and a number in Python?
I managed to fix it using isinstance()
x = 1
while x <= 100:
y = x
if x % 3 == 0:
y = 'Fizz'
if x % 5 == 0:
if isinstance(y, str):
y += 'Buzz'
else:
y = 'Buzz'
if x % 7 == 0:
if isinstance(y, str):
y += 'Foo'
else:
y = 'Foo'
if x % 11 == 0:
if isinstance(y, str):
y += 'Bar'
else:
y = 'Bar'
print y
x += 1
raw_input('Press enter to exit...')
Please tell me if this particularly bad code (which I have a habit of writing).
x = 1
while x <= 100:
y = str(x)
if x % 3 == 0:
y = 'Fizz'
if x % 5 == 0:
y += 'Buzz'
if x % 7 == 0:
y += 'Foo'
if x % 11 == 0:
y += 'Bar'
print y
x += 1
raw_input('Press enter to exit...')
It's quite simple.
You start off by defining X as an integer, then you increese it in a while loop.
At the very beginning of each iteration you define y = x which essentially tells python to set y into an integer.
Then depending on what x modulus <nr> you got, you add a string to the integer called y (yes, it is an integer as well).. This leads to an error because it's an illegal operation because of the way a INT and a WORD works, they're just different so you need to treat one of them prior to merging them into the same variable.
How to debug your own code: Try doing print(type(x), type(y)) and you get the differences of the two variables.. Might help you wrap your head around this.
The solution is y = str(x).
So, no.. Y is NOT a string to begin with
Because you redefine y each iteration of your while loop.
y = x <-- Makes Y a int, because that's what x is :)
Also, try using .format()
x = 1
while x <= 100:
y = x
if x % 3 == 0:
y = '{0}Fizz'.format(x)
if x % 5 == 0:
y += '{0}Buzz'.format(x)
if x % 7 == 0:
y += '{0}Foo'.format(x)
if x % 11 == 0:
y += '{0}Bar'.format(x)
print y
x += 1
raw_input('Press enter to exit...')
Another personal observation is that if x % 3 == 0 you replace y = ..., but in all other if-cases you append to y, why is this? I left it just the way you gave us the code but either do elif on the rest or why not concade on allif's