Generating Perfect Squares python - python

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.

Related

How do you mulitply via addition?

I'm having trouble trying to
compute x * y using only addition, printing the intermediate result for each loop iteration, and
printing the final number. This is my
teacher's example of what the output should look like
I could really use some help figuring it out,, I don't really understand how to yet.
Edit: I am a first time coder so I'm not very skilled, I do know the basics though (Also very new to this website) Here's what my code looks like so far:
x = int(input("Input positive x: "))
y = int(input("Input positive y: "))
z = 0
w = 0
if x < 0:
exit("Please input a positive number for x")
if y < 0:
exit("Please input a positive number for y")
def multiplier(number, iterations):
for i in range(1, iterations):
number = number + 3
print(number) # 12
multiplier(number=3, iterations=4)
My answer. Little shorter than the others.
Ok, try thinking of multiplication as adding a number with itself by x times. Hence, 3*4 = 3+3+3+3 = 12, which is adding 3 by itself 4 times
Replicating this with a for loop:
#inputs
x = 3
y = 4
#output
iteration = 1
result = 0
for num in range(4):
result += x
print(f'Sum after iteration {iteration}: {result}')
iteration += 1 #iteration counter
The resulting output will be:
Sum after iteration 1: 3 Sum after iteration 2: 6 Sum after iteration
3: 9 Sum after iteration 4: 12
The code is simple and straightforward,Check which number is bigger and iterate according to that number.
x = 3
y = 4
out = 0
if x > y:
for v in range(0,x):
out += y
print("after iteration",v+1, ":", out)
if y > x:
for v in range(0,y):
out += x
print("after iteration",v+1, ":", out)
print(x,"*", y, "=", out)
Use print to print inside the loop to show each number after adding and the number of iteration occurs.
Here is a start. This assume both arguments are integers 0 or more:
def m(n1, n1a):
n1b = 0
while n1a > 0:
n1b += n1
n1a -= 1
return n1b
n = m(9, 8)
print(n == 72)

why is a for loop not appending a list

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 prime number generator prints out every number

I made this prime number generator but it just prints every number (1, 2, 3...).
I am aware that there are other questions about this but I don't understand any of them. Here is what I have so far:
x = 1
y = 1
while x >= y:
if x % y == 0:
print(x)
x = x + 1
y = 1
else:
y = y + 1
y = 1
From your question, I think, it would be better try this way :
n = 10
i = 2
while i < n :
prime = 1 # reset the `prime` variable before the inner loop
for a in range(2, i):
if i%a == 0:
prime = 0
break
if prime:
print (i)
i += 1
Output :
2
3
5
7
There's something called a rubber ducky test where you explain your code, step by step, to a rubber duck. It forces you to walk through every step and you'll often find the error. In this case, think about what y is when you do x%y. You'll see that y=1 the first time this is called. So it will iterate through the loop incrementing x by 1 and y will remain set it 1. And x%y when y=1 will always be...

While loop creating

I am writing a program in Python that defines a function that takes a single argument. The function has to be a while loop the returns the largest power of 16 that it is equal to. However, I am not sure how to write the while loop.
Python Docs
while True:
n = input("Please enter 'hello':")
if n.strip() == 'hello':
break
so, in layman's terms
while <condition>:
...
I couldn't completely understand your question but here's how to do a while loop to get the x to the 16th power of an input:
def loop_function(x):
y = 1
start = x
while y != 16:
result = start * x
start = result
y += 1
return result
print loop_function(3)
The above code will return the answer to 3^16 which is 43046721
You can even make this a broader function two arguements
def loop_function(x, y):
z = 1
start = x
while z != z:
result = start * x
start = result
z += 1
return result
print loop_function(3, 2)
The above code will return 9 i.e 3^2

Sum of numbers from 1 to 5 in loop

To simplify my scenario, I am redefining my question here.
I want to add numbers from 1 to 5 in loop. X should be 1,2,3,4,5. and start with Y as 0. Y = X + Y should give the sum of 1 through 5.
Requirement: I want to start y as 0 and want y to hold latest add_sum value.
Expected output:
1st iteration: y = 1 (x = 1, y = 0)
2st iteration: y = 3 (x = 2, y = 1)
3st iteration: y = 6 (x = 3, y = 3)
...
...
so on
I am new to python coding, Can anyone help me for this?
Use reduce
reduce(lambda x, y: x + y, range(6))
As mentioned in the comments, fixing your syntax and running your code seems to work just fine.
def read_num():
for num in range (1,5):
x = num
add_sum(x)
def add_sum(x):
global y
y = x + y
print ("y =", y)
y = 0
read_num()
If you want x to be 1 thru 5 inclusive, you have to use range(1,6) instead
y = 0 # Assign value 0 to variable 'y'
for x in xrange(1, 6): # Cycle from 1 to 5 and assign it to variable x using iterator as we need just 1 value at a time
print '(x=%s, y=%s)' % (x, y) # Display value of 'x' & 'y' variables to user for debug & learning purpose
y += x # Adding x to the y.
print 'y=%s' % y # Display result of sum accumulated in variable 'y'
Edit: added comments to the code as requested in comments.

Categories

Resources