range function with if-else statment in python - python-3.x

when I enter
45 200
why I am getting no
and the reason is the if-else statement is coming false
but I don't know why
T = int(input("i"))
for _ in range(T):
X,Y = map (int, input().split(" "))
if Y in (X, X+200):
print("yes")
else:
print("no")

if Y in (X,X+200) will check if Y exists in the tuple consisting of the values X and X+200. You might be looking for the python range function.
if Y in range(X,X+200) should do what you need.

Related

Make list comprehension of this for loop, Python

I'm matching first value of a pair in tuples, and returning the second value if the first value matches across tuples. The code I wrote is working but I want to make it more pythonic, this code looks overly long:
def intersect(A, B):
setx = set()
for i in A:
i, y = i[0], i[1]
for x in B:
if i in x:
x = x[1]
setx.add((y,x))
print(setx)
return setx
Now I execute function in interpreter and get the result I want:
intersect(
{ (8, 'huit'),
(10, 'dixA'),
(12, 'douze')},
{ (5, 'cinq'),
(10, 'dixB'),
(15, 'quinze')})
and the output shows:
{('dixA', 'dixB')}
I try to get fancy and try to clean up the code:
def intersect(A, B):
setx = set()
for i in A:
i, y = i[0], i[1]
c = x for x in B if i in x
#x = c[1]
setx.add((y,x))
print(setx)
return setx
The above code gives me an invalid syntax error. When I create a list comprehension
[x[1] for x in B if i in x]
It returns a list and I cant add lists using the add method of set. Would someone be able to help me clean up my code a bit?
Interestingly this worked, I haven't used the double ++ sign in a while:
def intersect(A, B):
setx = set()
for i in A:
for x in B:
if i[0] == x[0]:
setx.add(i[1]), setx.add(x[1])
return setx

name 'count' is not defined in python

I have this code in python and I am trying to make a counter for the iteration of the binary search (yeah I know it is incomplete...), but I am stuck with the variable inside the function, when i try to print the variable count I get this error
name 'count' is not defined in python
can someone explain why i get this error?
import csv
def binarySearch(arr, l, r, x):
count=0
while l <= r:
mid = int(l + (r - l) / 2)
# Check if x is present at mid
if arr[mid] == x:
return mid
# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1
# If x is smaller, ignore right half
else:
r = mid - 1
# If we reach here, then the element
# was not present
return -1
with open('bl_printed_music_500.csv', newline='', encoding="utf-8-sig") as csvfile:
reader = csv.DictReader(csvfile)
arr=[]
for row in reader:
if row ["Publication date (standardised)"] != "":
arr.append(int(row["Publication date (standardised)"])) #create list for searching
list.sort(arr) #list must be sorted to work
#print (arr)
x = 1850 #year to search
# Function call
result = binarySearch(arr, 0, len(arr) - 1, x)
found = False
if result != -1:
found = True
print(found)
print(count)
I think it's because you defined count in binarySearch but try to use it outside of the method. Try using a global variable (define it outside of binarySearch), it should work.
You can return count as well.
For example:
def myFunc():
x = 5
y = 10
return x,y
a, b = myFunc()
print(a)
print(b)
This will be:
5
10
Note that, I could have written x, y = myFunc(). These x and y are not the same as the ones inside myFunc(). The latter are local to the function.
In your code, you can return your local count variable:
return mid, count #(A)
return -1, count #(A)
And get its value by:
result, count = binarySearch(arr, 0, len(arr)-1, x) #(B)
Again, these two count variables, (A) and (B) are different variables with different scopes.
See, for instance:
https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
Alternatively, if a global variable, as suggested in the other answer, suits you best, you can see an example of its usage in the link.

Checking if three inputs form a triangle

So I want to take three inputs from the user and check whether they form a triangle.Now I want my program to check using any three random values of of the given inputs and check whether a + b > c.Here is my code for that :
def check_triangle(a, b, c):
a, b, c = [float(i) for i in input('Enter the stick lengths: ').split(' ')]
x, y, z = [int(num) for num in [a, b, c]]
list_1 = [x, y, z]
def is_triangle(x, y, z):
for i in list_1:
if (list_1[i] <(list_1[i+1] + list_1[i+2])):
print("Yes")
else:
print("No")
check_triangle(a, b, c)
But I am getting no output.
What is the error
Your are getting no output, because you defined a function inside a function, but you are calling only the first one. So second one is defined but never executed. What you want to do to execute the second one is to add a function call at the end of the first one, so in your case it would be:
def check_triangle(a, b, c):
a, b, c = [float(i) for i in input('Enter the stick lengths: ').split(' ')]
x, y, z = [int(num) for num in [a, b, c]]
list_1 = [x, y, z]
def is_triangle(x, y, z):
for i in list_1:
if (list_1[i] <(list_1[i+1] + list_1[i+2])):
print("Yes")
else:
print("No")
is_triangle(x,y,z)
Indentation can be messed up, because I'm answering on my phone, sorry for that.
Also, from what I can see you will be getting list index out of range error at this line.
if (list_1[i] <(list_1[i+1] + list_1[i+2]))
That is happening because your i is actually an element of your list as defined in the line below, not an index, but you are trying to get an element from the list by it's index with the syntax my_list[index].
for i in list_1
What you want to do instead of the for loop mentioned above is iterate in the range of it's length, meaning iterate over possible indexes in the list, is done like so:
for i in range(len(list_1))
I notice a few other things in your code and a lot of room for improvement, but I hope you can deal with the rest yourself!
you are taking input from the user so you do not need to pass any arguments for your function check_triangle:
def is_triangle(x, y, z):
if x < y + z:
print("Yes")
else:
print("No")
def check_triangle():
x, y, z = map(int, input('Enter the stick lengths: ').split())
is_triangle(x, y, z)
check_triangle()
or you can simplify you code like:
def is_triangle(x, y, z):
print('Yes' if x < y + z else 'No')
is_triangle(*map(int, input('Enter the stick lengths: ').split()))
To begin, since a, b and c are variables input by the user, they do not need to be input into the function as arguments. In fact, because they are not defined before being given as arguments to the function, it is causing the function to raise a NameError with the message name 'a' is not defined when it is called. To fix this issue, you can remove a, b and c as arguments to your function in both its definition and its usage.
At this point, the function will run, but even if the user inputs the numbers in the format which your program is expecting (i.e. separated by single spaces - which is not explicitly specified to the user) the portion of your program that evaluates your target condition will not run because it is contained within the function is_triangle(x, y, z) which is not called. This function can be eliminated and your test condition can be evaluated within the main function. Additionally, there is no need to loop through the elements in the list as you can access its elements directly to evaluate your target condition.
Here is the code with these changes made:
# since a, b and c are given by the user, they do not need to be arguments to the function
def check_triangle():
a, b, c = [float(i) for i in input('Enter the stick lengths: ').split(' ')]
x, y, z = [int(num) for num in [a, b, c]]
list_1 = [x, y, z]
# evaluate your test condition directly within the main function. no secondary function is necessary
# as you want to check that c < a + b, access those elements directly by using the index of the list. no loop is necessary
if list_1[2] < list_1[0] + list_1[1]:
print("Yes")
else:
print("No")
# since a, b and c are not defined before running the function, they cause it to raise a NameError if they are given
check_triangle()
This code will run and evaluate your target condition. However, this condition (that c < a + b) does not (to my knowledge) actually mean that something is a triangle. Good luck and happy coding!

Is there a better way to write this code, should I have the global variables before the function prototype instead?

This code is supposed to take two inputs from the user and then sum them. If the result is between 15 and 20, it should print '20' and if it is any other, it should print the exact summed answer.
def mysum (x,y):
total = x+y
if total in range (15,20):
print ('20')
else:
print (total)
x = int(input("Input the number: "))
y = int(input("Input the number: "))
mysum(x,y)
I'm not sure if you can say this is better, but here are 2 ways to do it differently:
Here, I do the exactly the same as you did but a bit shorter: (maybe less clear too?)
def mysum(x, y):
print("20" if 15 <= x+y < 20 else str(x + y))
x = int(input("Input the number: "))
y = int(input("Input the number: "))
mysum(x,y)
There, I put everything inside the mysum function (and used the shorther syntax of the first way):
def mysum(): # No arguments
x = int(input("Input the number: "))
y = int(input("Input the number: "))
print("20" if 15 <= x+y < 20 else str(x + y))
mysum()
Note that the mysum function no longer requires arguments this way. Maybe you'd prefer it that way, maybe you don't (because you want to re-use x and y later in the code, for instance).

tuple unpacking in python using for loops?

l=[(1,2),(3,4),(5,6)]
for (a,b) in list:
for i in range(len(list)):
if i%2==0:
print(b)
break
else:
print(a)
break
output-
2
4
6
expected output-
1
4
5
PLEASE correct it!
You may want to be more specific about what you want to achieve.
Based on your "expected output", I assume you want the 1st element when the index is even and the 2nd element when the index is odd.
l=[(1,2),(3,4),(5,6)]
for idx, (x, y) in enumerate(l):
val = x if idx%2==0 else y
print(val)

Resources