Python While Loop, Confused on "or" operator [duplicate] - python

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 4 months ago.
Basically I have a while loop that I want to run until either userWins or botWins equal 5. But with this code it will only stop when the variable "userWins" equals 5. I'm newer to python so I don't know if there is a different operator I am supposed to use to make this happen. Thank you for your help.
while userWins < 5 or botWins < 5:

So you want to run while both userWins and botWins are lower than 5.
You should use while userWins < 5 and botWins < 5: that's not a programming problem but a logic problem.

Related

Strange behavior of if elif condition in Python [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 6 months ago.
I'm new to python and currently working on implementing Luhns algo in python. I'm already done with calculation of checksum. Now I'm facing challenge in identifying the card company using if and elif statement.
Below is my code block to identify the card company. The problem I'm facing is else if statements are not getting executed even if the conditions are true and, in some cases, even ELSE within IF is not being executed. This happens whenever there's an OR condition to compare with multiple values.
I've tried moving the full conditions within parenthesis((first_2 == (34 or 37) and length==15) but the result is same.
Can't we use multiple conditions in if or elif statement OR is there any other way compare multiple values for if statement?
Thank You
Code>>>
if (digit+number_add2) % 10 ==0 and length >= 13:#check if remainder is Zero
if first_2 == (34 or 37) and length==15:#Check first 2 digits and length
print("AMEX")
elif first_2 == (51 or 52 or 53 or 54 or 55) and length==16:
print("MASTERCARD")
elif first_1== 4 and length == (13 or 16):
print("VISA")
else:
print("INVALID1")#Invalid if number passes Luhns algo but card is not from Visa,MC,Amex
else:
print("INVALID2")#Invalid if card number does not adhere Luhn's algo

Negative one to the power of two question - i.e. (-1)**2 [duplicate]

This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 1 year ago.
Can someone explain the difference between these two expressions in Python:
(-1)**2 == 1
-1**2 == -1
Why do the parentheses change the outcome?
The parentheses means the whole value inside is will be raised to the power 2.
(-1)**2 == 1
So -1*-1 is 1
No parentheses means the - will be taken out of the equation and added to the end of the answer.
1) -1**2
2) 1**2
3) 1
4) -1
Python handles this the same way the world does :)

Split string into two integers, python [duplicate]

This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want

How to find the absolute value of a complex function? [duplicate]

This question already has answers here:
Complex numbers in python
(3 answers)
Closed 4 years ago.
I want to find the absolute value of something like 'i' but when I type 'abs(1)' it says 'i' is not defined. What do I do?
In python to find the absolute value of a complex function you use j instead of i.
abs(a+bj) # General Format
abs(0+1j)
>> 1
Or you could define i as the square root of -1 and use it instead
i = (-1) ** 0.5
abs(i)
>> 1

How short circuit code works in python [duplicate]

This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
Closed 6 years ago.
This looks like a short circuit way of writing code but I just can't understand it. Is there a specific way to read this kind of short circuit.
e.g:
n = n and int(n)
n = n or int(n)
and conditions return the last truthy value or the first falsy one. So if n is falsy, n will remain whatever value it was. If n is truthy, it will be cast to an int.

Categories

Resources