I am very new to Python.
I'm trying to create a loop that compares int, between a range.
while counter < N:
x = randn()
if x >= 0 and <=1:
print('0-1')
counter = counter + 1
elif x < 0 and < -1
print("0- -1")
counter = counter + 1
I keep getting a syntax error on the <=
File "<ipython-input-35-1d74b6e80ea0>", line 9
if x >= 0 and <=1:
^
SyntaxError: invalid syntax
Any help on what I am missing would be greatly appreciated
The correct syntax is:
if x >= 0 and x <= 1:
The reason for your confusion is because you're writing it out as you would explain it to a person. X has to larger or equal to zero and smaller or equal to one.
In python however, these are two separate conditions, which need to be written out in full: x >= 0 and also x <= 1.
Alternatively, you have the option of combining the operators into a single condition like so:
if 0 <= x <= 1
Merging them this way turns the inequality into a single (compound) condition.
You should try writing it as if x >= 0 and x <= 1:. The and connects two separate statements, so you need to write the comparisons separately.
Related
This is what I have done:
Can anybody tell me where I went wrong?
num = 0
x = 1
while x <= 500:
num += x
x += 5
print(num)
Your indentation is also wrong. It needs to be like this
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
But, if you want to print only the final sum, you can use the print statement outside loop
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
Logic: We have to use while loop to check every number. We have to check whether the number is divisible by 5 or not, for that, we have to put if condition and keep on adding +1 to go from 0 to 500. If the condition is satisfied, add to to a variable whose initial value is 0. You code:
x=0
r=0
while x<=500:
if x%5==0:
r+=x
x+=1
print(r)
Or you can do it this way, start the initial value from 0, updating value of x should be 5, add it to variable whose initial value is 0. You alternative code:
x=0
r=0
while x<=500:
r+=x
x+=5
print(r)
The second code will help you rectify your personal code. Have a look
Yet another way:
x = 0
acc = 0
while (x := x + 5) <= 500:
acc += x
Here we use an assignment operator inside while() to get the value while incrementing it at the same time.
Python newbie here.
Let's say we have an array which contains 10 random integers.
I want to check if each value of the integers is 0<= x <= 9.
So for example somehow like this:
if 0 <= n[:11] <=9:
print('correct')
'n[:10]' is treated like a list so I can't compare it with 0 and 9.
Is there an elegant way to check the range of items in that array?
I don't want to code something like:
if 0 <= n[0] and n[1] and ... n[9] <=9:
thanks for your help
Check this out:
This returns True if and only if ALL of the numbers in the list n are at least 0 and at most 9, (in range(0, 10))
all(i in range(0, 10) for i in n)
if 0 <= min(n) <= max(n) <= 9:
Could use and instead of <= in the middle, not sure which I like better.
What you want is the check the equality for every elements:
all(0 <= item <= 9 for item in n)
If you first sort the list you only have to check the first and last value, like this:
sorted_n = sorted(n)
if sorted_n[0] > 0 and sorted_n[-1] < 10: # returns True or False for whole list
I'm currently working on a small script that aims to find taxicab numbers. There is only one small problem, the for loop doesn't increment the variable x and is stuck looping forever. I'll paste the relevant part of the code below:
n = int(10)
counter = int(0)
m = int(m)
x = int(1)
y = int(n**(1/3))
cheatsheet = []
while counter in range(0,m):
for x in range(1,y):
if x**3 + y**3 < n:
print('less than')
print(x,y,n)
continue
elif x**3 + y**3 > n:
print('greater than')
y -= 1
continue
elif x > y:
if n in cheatsheet:
print('counting')
counter = counter+1
#checking if n occurs in the list, if so it will add 1 to the counter so that the program will know when to stop
n = n + 1
y = int(n**(1/3))
x = 1
print('break1')
#resetting values and bumping n so that it will continue the quest to find 'em all
break
else:
if x and y == 1:
#this is an error correction for low n values, i mean really low it might be redundant by now
n = n + 1
y = int(n**(1/3))
x = 1
print('break2')
break
cheatsheet.append((n,x,y))
print(cheatsheet)
This yields the following result in a terminal window:
image
Note that I killed the process by myself, the program did not.
As you can tell, the script just loops around and prints 'less than' and the values for x,y,n.
Help is greatly appreciated!
EDIT: the variable m is given by the user, but not included in this bit of code.
This has a number of issues wrong with it I'm not willing to post functional code but I will point some of the issues out.
For starters:
n = int(10)
isn't required, not an error but redundant. Use n = 10 with the same effect.
Then:
while counter in range(0,m):
will always evaluate to True. m is *never modified so the membership test always succeeds, maybe you need to re-evaluate your looping.
for x in range(1,y):
This will assign x the value 1 all the time. y evaluates to 2 by your arithmetic (int rounds floats to the floor, i.e int(2.9) -> 2) so either use math.ceil or add one to your y.
Appart from that you re-assign variable names all the time inside your loops, that's confusing and might lead to unexpected behavior.
So, I have a script in python (2.7) that makes a list of 1000000 numbers between 1 and 5000 and then runs a for loop over the entire list checking each element against multiple embedded if statments.
ex:
i = 0
for number in random_list:
i += 1
if random_list[number] <= 3000:
i += 1
if random_list[number] <= 300:
i += 1
if random_list[number] <= 30:
i += 1
if random_list [number] <= 3:
i += 1
break
print i
the whole point being I want to see how many "checks" occur over the entire list.
unfortunately my i value is less than the total number of elements in the list which should not be the case since each element should be checked at least once. I'm getting something in the range of 1000's to 10000's.
I'm sure there's a more pythonic way of doing this, so any advice on how to achieve what I'm trying will be greatly appreciated.
extra = 0
values = (random_list[number] for i,number in enumerate(my_numbers))
for v in values:
extra += sum(v < x for x in [3000,300,30,3])
if v < 3:break;
print i+extra
maybe ...
I have the following function:
def InRange(number):
return 5 <= number >= 1
I want this to say false if the number is not within the range of 1 to 5 using a chain comparison, but cannot seem to get this right.
Any suggestions?
You want it like this:
def InRange(number):
return 1 <= number <= 5
Note that you could also do:
def InRange(number):
return 0 < number < 6
Use this:
1 <= number <= 5
From docs:
x < y <= z is equivalent to x < y and y <= z, except that y is
evaluated only once (but in both cases z is not evaluated at all when
x < y is found to be false).
Your (incorrect)expression is actually equivalent to:
number >=5 and number >= 1
So, it is going to be True for any number between 1 to infinity:
Alternatively you can do (it seemed appropriate based on the function's name):
def InRange(number):
return number in range(1, 6)
For large numbers you should use:
def InRange(number):
return number in xrange(1, 10000000)