For some reason, when I try to set a while function, it doesn't do what I want.
For example:
import random
x = 0
while x <= 10:
print random.randint(1, 100)
x += x + 1
This only runs four times. But if I change it to while x <= 1000: it runs ten times. Any idea why?
EDIT
I am sorry, But think this is a legitimate question. Please stop downvoting me! Next time I will do more research into the syntax for problems as simple as this. Sorry.
To increment x by 1 you should use
x += 1
or
x = x + 1
You have combined them into x += x + 1, which adds x+1 to x each time.
Your problem is x += x + 1 is essentially adding x + (x + 1). Simply remove the initial +.
You are adding x + 1 to x. It should be x+=1, that makes you finish sooner:
Start: x = 0
After first iteration: x + x + 1 = 0 + 0 + 1 = 1
After second iteration: x + x + 1 = 1 + 1 + 1 = 3
After third iteration: x + x + 1 = 3 + 3 + 1 = 7
After fourth iteration: x + x + 1 = 7 + 7 + 1 = 15, which is greater that 10 and stops.
You are multiplying x by 2 and adding 1, so it's not an error just is the answer to another question.
Related
So I'm given the following diagram:
And I'm being asked to find the area of each polygon given n. The area is basically the sum of all blue squares since each square has an area of 1. So when n = 1, the area is one. When n = 2, the area is 5. Because of the relationship between each polygon, I know that I could knock this down using set theory.
n Area(n)
1 1
2 A(n-1) + (4 * (n-1)) = 5
3 A(n-1) + (4 * (n-1)) = 13
4 A(n-1) + (4 * (n-1)) = 25
5 A(n-1) + (4 * (n-1)) = 41
However, I didn't have as much luck trying to represent this in code:
def shapeArea(n):
prev_output = 0
output = 0
if n == 1:
output = 1
elif n > 1:
for i in range(n):
prev_output = n-1 + (4 * (n-1))
output = prev_output + (4 * (n-1))
return output
For example: For n = 2, I'm getting an output of 9 instead of 5.
You were close :-)
Here are the small fix-ups:
def shapeArea(n):
output = 1
for i in range(1, n):
output += 4 * i
return output
Running this:
for n in range(1, 6):
print(n, shapeArea(n))
Gives this output:
1 1
2 5
3 13
4 25
5 41
of course with Gauss's theorem the code would look like this:
def ShapeArea(n):
return 2*(n**2) - (2*n)+1
In a recursive solution, it is much simple from your logic.
def shape_area(n):
if n == 1:
return 1
return shape_area(n-1) + 4*(n-1)
You can use Gauss' trick to make a closed-form formula:
2*Area(n) =
1 + 4 + 8 + 12 + ... + 4(n-1)
+
1 + 4(n-1) + 4(n-2) + 4(n-3) + ... + 4
--------------------------------------------------
2 + 4n + 4n + 4n + ... + 4n
= (n-1)4n + 2
= 4n^2 - 4n + 2
So Area(n) = 2n2 - 2n + 1
def shape_area(n):
return sum([1] + [4*i for i in range(1, n)])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
m = 0
x = 1
while x < 4:
y = 1
while y < 3:
m = m + x + y
y = y + 1
x = x + 1
print(m)
The output is supposed to be 21 but i dont get it , what am i missing? a little help please
m = 0 and x = 1
Since x < 4 it goes inside the while loop where y is set to 1
Since y < 3 it goes inside the nested while
m becomes m + x + y = 0 + 1 + 1 = 2 and y becomes y + 1 = 1 + 1 = 2
Back to the loop condition: y < 3? Yes! Because y = 2. So it goes again inside the while
m becomes m + x + y = 2 + 1 + 2 = 5 and y becomes 3
Back again to the loop condition: y < 3? No! 3 is not less than 3, so the while is now skipped
x becomes x + 1 = 1 + 1 = 2
Back to first while condition: x < 4? Yes! Because x = 2. So it goes inside the loop again
Back to step 2.
When x finally becomes 4, the while loop will terminate and m will be printed.
Let's have a "graphical" representation.
Consider:
x values starting with 1 and growing from left to right (we don't care what's after 3: while x < 4)
y values (!!! for each x !!!) starting with 1 and growing from top to bottom (we don't care what's after 2: while y < 3)
x values are displayed using the "normal" font style, while y ones are displayed in "italic"
Everything that we care about is displayed in "bold" (actually what is not in "bold" won't even be computed by the program, I'm placing those values here, just for clarity's sake):
x values (x ∈ {1, 2, 3})
y values (y ∈ {1, 2})
x row is displayed twice, since for each y, x is added to the sum
The sum:
Is under the separation line and starts from 0
Each number is the sum (consider it a partial sum) of the numbers (in bold) on that column (above it) - they correspond to one x iteration and they contain all y iterations for that x
At the end, we add those values - for all x iterations - and get the final value
x (→): 1 2 3 4 5 6 ...
y (↓): 1 1 1 1 ...
x (→): 1 2 3 4 5 6 ...
y (↓): 2 2 2 2 ...
y (↓): 3 3 3 3 ...
y (↓): ... ... ... ... ...
sum: 0 + 5 + 7 + 9 = 21
Please help
Below is the code
start = 3
end = 5
for x in range(start, end + 1):
print x
#and
#print iterate from 0
I am looking here, x will print 3 4 5
and I also need to print 0 1 2 that first time enter to loop print 0 and second time enter to loop print 1 and so on.
Please help
python has enumerate for just this:
start = 3
end = 5
for i, x in enumerate(range(start, end + 1)):
print(i, x)
which prints:
0 3
1 4
2 5
start = 3
end = 5
counter = 0
for x in range(start, end + 1):
print x
#and
#print iterate from 0
print counter
counter += 1
I am trying to get this code to calculate 5 and print numbers by 5's with a while statement of 7, so I want it to loop through, generating a different number 7 times; however, when it gets to a number over 10, I want it to start back over at 0 and ignore the 10.
This is my code:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
The code prints the first number, but freezes on printing any others. Why isn't this working?
Your code is not in the loop. Python's code blocks are created with indents:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
Is this the result you were trying to achieve:
import random
x = random.randint(1,9)
for i in range(1,8):
print x
x += 5
if x >= 10:
x -= 9
This generates a random number, and adds 5 until it passes 10, then subtracts 9, and it does this seven times. If I understand your question correctly, this is what you were trying to do. Please correct me if I am wrong.
no this is not what I was trying to do here is what I am trying to do.
counts = 0
r = 0
firstpickplusfive = 7
p = firstpickplusfive + 5
while r < 3:
firstpickplusfive = p + counts
if firstpickplusfive > 6:
firstpickplusfive = firstpickplusfive + counts - 10
if p > 9:
p = firstpickplusfive + counts
print firstpickplusfive
counts = counts + 1
r = int(r) + 1
it works alone, but when I add it to the script I am trying to write it doesn't work...if there is a simpler way to do it I would appreciate knowing it.
ie.
number = number + 5 + 0
then
number = number + 5 + 1.....ect which
example 7 + 5 + 0 = 12,
7 + 5 + 1 = 13.........
if the number is equal to 10 then I want it to drop the tens place and keep the 1's place
example 7 + 5 + 0 = 2,
7 + 5 + 1 = 3
Here is an easier method:
for i in range(1,4):
num = 7
num += 5
num +=(i-1)
if num >=10:
num -= 10
print num
i+=1
Try this in your script, does it work?
I have a task to make a program that will sum the first 100 Fibonacci numbers. I checked my output in Python, and my output in QBasic 64 and they aren't same. I checked with different inputs also.
Input: 10
Output: 89
-----------
Input: 100
Output: 573147844013817084101
Is it correct ?
Here is my code:
n = int(input())
print()
p = 0
d = 1
z = p + d
print(str(p) + ' + ' + str(d) + ' = ' + str(z))
for i in range(n - 2):
p = d
d = z
z = p + d
print(str(p) + ' + ' + str(d) + ' = ' + str(z))
print('Sum:', z)
EDIT: Code edited again, check it now. I just found on Wikipedia.. It depends from what number you start the loop. So if I use (0, 1, 1, 2, 3, 5, 8, 13, 21, and 34) as first 10 Fibonacci numbers, the sum is going to be 88, not 89.
The sums of the first ten and 100 fibonacchi number would be 88 and 573147844013817084100, respectively:
>>> cache = {}
>>> def fib(n):
if n == 0: return 0
if n == 1: return 1
if not n in cache:
cache[n] = fib(n - 1) + fib(n - 2)
return cache[n]
>>> sum([fib(i) for i in range(10)])
88
>>> sum([fib(i) for i in range(100)])
573147844013817084100
In your loop you are already starting the iteration at the 3rd position, since you set. So set your range to (n -2).
0: 1
1 : 1
2 : 1
3 : 2
4 : 3
5 : 5
To get the sum of the Fibonacci numbers, using zero as the first in the series, you need to do this:
def run_it(n):
N2 = 0
N1 = 0
N = 0
z = N
for i in range(n):
print(N,z)
N2 = N1
N1 = N
if N is 0: N = 1
else: N = N1 + N2
z = z + N
run_it(int(input('Number: ')))
To calculate the sum using one as the start of the series, change the initial value of N from zero to one.