I am doing a simple script for self learning in python where the scripts in turn finds the 1000th prime number but I get a syntax error.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:(counter = counter + 1 and integer = integer + 1)
print (y)
when it comes to the ='s assignment right after the ELSE operator and I don't understand why it won't let me add one to both the counter and integer when this has worked in other iteration scenario
In python you can't make an assignment inside an expresion, to avoid misspellings between = and ==. So you must do that in two lines:
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)
try this
else:
counter = counter + 1
integer = integer + 1
In python, assignment to variable has no Boolean value. and mean Boolean operator not do this and this.
so you need to split the statements.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)
Related
I am trying to arrange slashes in print statement to Graph like this
Below is skeleton of my code
y = input("Enter numbers (seperated by space): ")
y_arr = []
y_list = y.split()
for n in range(len(y_list)):
y_arr.append(int(y_list[n]))
space_no = 0
for x in range(len(y_arr)):
value = y_arr[x]
for z in range(value):
if (x%2 == 0):
print (" "*space_no,"\\")
space_no += 1
else:
print (" "*space_no,"/")
space_no += 1
Input statement take numbers and put it into list, then the slashes are generated based on the value of list item. For exa: if value is 2 then 2 slashes will be printed. Even index values go up, odd index values go down. Print statement is printing every character in new line but I want to arrange them like shown in graph.
How to achieve this?
code edited to Python 3
That sounds like homework, but here it is.
y_list = '0 3 1 2 3 6 2 3 6 2 3'.split()
y_arr = []
for n in range(len(y_list)):
y_arr.append(int(y_list[n]))
def cumulative(lists):
cu_list = []
length = len(lists)
cu_list = [sum(lists[0:x:1]) for x in range(0, length+1)]
return cu_list[1:]
space_up = 0
space_dn = 0
last_idx = -1
lines = ['']*max(cumulative([-x if i%2 == 0 else x for i,x in enumerate(y_arr)]))
for x in range(len(y_arr)):
value = y_arr[x]
for z in range(value):
if (x%2 == 0):
lines[last_idx] += (" "*space_dn+"\\")
space_dn = len(lines[last_idx])-len(lines[last_idx-1])
last_idx -= 1
space_up = 0
else:
last_idx += 1
lines[last_idx] += (" "*space_up+"/")
try:
space_up = len(lines[last_idx])-len(lines[last_idx+1])
except IndexError:
pass
space_dn = 0
print('\n'.join(lines[::-1]))
for x in predRslt:
for y in actRslt:
if x == y and x =='1':
trueP += 1
elif x == y and x =='0':
trueN += 1
elif x != y and x == '1':
falseN += 1
elif x != y and x == '0':
falseP += 1
charNum += 1
totalActN = trueN + falseP
totalActP = falseN + trueP
totalPredN = trueN + falseN
totalPredP = trueP + falseP
print falseP
cmp_rslt('0110101001','1100100101')
actual output: 25
expected output: 2
Im trying to go through each string sequentailly from the beginning and compare the results. Then increment the corresponding TrueN, TrueP, FalseN, or FalseP.
for some reason, I keep getting an output of 25 when I should be getting way less than that because there's only 10 comparisons I should be making
you should iterate using the index of the first string like this:
for idx, x in enumerate(predRslt);
y = actRslt[idx]
if x == y and x =='1':
trueP += 1
elif x == y and x =='0':
trueN += 1
elif x != y and x == '1':
falseN += 1
elif x != y and x == '0':
falseP += 1
In the below code, the loops don't seem to be working.
powers = []
x = 0
y = 0
z = 1
for x in powers:
powers.append([])
for y in powers[x]:
powers[x].append(z ** y)
z = z + 1
if z < 1001:
continue
else:
break
x = x + 1
y = y + 1
z = 1
print(powers)
However, when I run this in the terminal, it simply returns an empty list for powers. It doesn't show an error message.
Please help.
your code is badly formatted, so please consider formatting, anyway some of your errors are code-comments.
powers = []
x = 0
y = 0
z = 1
for x in powers: #here you're overriding former declaration of x, also powers is empty so for doesn't have a single run
powers.append([]) ##code is not indented so this istruction execute only once
for y in powers[x]: ##powers[x] is an empty list
powers[x].append(z ** y)
z = z + 1
if z < 1001:
continue
else:
break
z = 1 ## this is outside the cycle
x = x + 1
y = y + 1
print(powers)
(Python 3.x)
z=[]
x=0
while 1==1:
x=x+1
y=1
z.append(x)
while y==1:
a = 0
b = 0
if z(a)==x:
print(x)
y = 2
elif x%z(a)!= 0:
a = a+1
elif b == 2:
y = 2
else:
b = b+1
So, I made a code to find all the prime numbers until python crashes. However, it relies on z(a) for it to work. The idea is that as "a" changes, it moves on in the list.
"z(a)" is where the error lies, so does anyone know a way to fix this?
z is a list. You can approach values inside it by indexes using the z[a] operator (and not z(a) which assumes a function call with a as parameter).
I've taken the liberty of using boolean variables, += operators and unpacking values:
z = []
x = 0
while True:
x += 1
y = True
z.append(x)
while y:
a, b = 0, 0
if z[a] == x:
print(x)
y = False
elif x % z[a]: a += 1
elif b == 2: y = False
else: b += 1
I believe that's what you want to achieve (infinitely incrementing prime generator):
def prime (n): return not any([n % i == 0 for i in range(2, n)])
x = 0
while True:
if prime(x): print(x)
x += 1
I am trying to write the simplest code possible that will continuously print out Perfect Squares. My code so far reads this
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
When I run it, I get a syntax error. The red bit is on the "final" in "print final"
Apologies if it is a really basic error, but I'm stumped.
Thanks for taking the time to read this
I assume you're using Python 3. print is now a function in Python 3.
Do print(final) instead of print final.
Also, it seems like your x and y values are holding the same thing. Why not discard one of them and use just one?
x = 1
while True:
final = x * x
print(final)
x = x + 1
Or better yet, use the builtin exponentiation operator **.
x = 1
while True:
final = x **2
print(final)
x += 1
Also, your code seems to be going into an infinite loop. You may need a condition to break it.
For example, if you want to break when x reaches 10, just add a condition in the while loop, like follows:
x = 1
while True:
final = x **2
print(final)
x += 1
if x == 10:
break
OR Specify a condition in the whilestatement, like follows:
x = 1
while x < 10:
final = x **2
print(final)
x += 1
OR Use a for loop.
for i in range(10):
print(i**2)
In Python 3, print is no longer a statement but a function, so you must enclose what you want to print in parentheses:
print(final)
Here's a link about the function.
You also have an IndentationError, y = y + 1 should be given a space.
And you can simplify that to y += 1 (which is the same thing, in regards to integers)
You can also add a condition to the while-loop:
x = 0
while x < 5:
print x ** 2
x += 1
Prints:
0
1
4
9
16
I would reccomend using Python 3 if you're not already. Also, as x and y are the same value at all times you only need one of them. So instead of reading:
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
You should write:
x = 1
while True:
final = x * x
print(final)
x = x + 1
Hope this helped!
Jake.