Having some trouble with a little bit of Python Homework:

In summary, the problem is about finding the sum of an arithmetic progression with a given distance and positive integer n. The distance is the difference between any two successive numbers in the sequence, and the sum should include all elements from 1 to n. The solution involves using a "for" statement and adding the distance to a given integer within the range of 1 to n. The final code should be: "sum = 0" "for i in range(1, n+1, distance):" "sum += i".
  • #1
Spencero94
4
0

Homework Statement


The problem shows as follows:
An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.

Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 25 because 1+3+5+7+9 = 25 .


Homework Equations



This is definitely a "for" statement, and distance is added to a given integer i within the range of (1,n)

The Attempt at a Solution


So far i have the following written, but it seems to be telling me to use a "+" sign somewhere in my script:
sum = 0
i = 1
for i in range (1, n):
i += distance
sum += i
 
Technology news on Phys.org
  • #2
Try this one instead:
Code:
sum=0
for i in range (1,n,distance):
    sum+=i
 
  • #3
Almost, just tweek it slightly.

sum=0
for i in range(1,n+1,distance):
sum += iviola!
 

What is Python homework?

Python homework is a set of assignments given to students to practice and apply their knowledge of the Python programming language. It typically involves writing code to solve problems or complete tasks using the concepts and syntax of Python.

Why is Python used for homework?

Python is a popular and widely-used programming language, known for its simplicity, readability, and versatility. It is commonly used for homework assignments because it is beginner-friendly and allows students to quickly learn and apply programming concepts.

Can someone help me with my Python homework?

Yes, there are many resources available to help with Python homework. You can seek help from your teacher, classmates, or online tutorials and forums. It is important to ask for help when you are struggling and to actively engage in the learning process.

How do I get better at Python homework?

The best way to improve at Python homework is to practice regularly. Take the time to understand the concepts and syntax, and try to solve problems on your own before seeking help. Also, seek feedback and actively look for ways to improve your code.

What should I do if I am stuck on my Python homework?

If you are stuck on your Python homework, take a break and come back to it with a fresh mind. Sometimes, taking a step back can help you see the problem in a new light. You can also try breaking the problem into smaller parts and tackling them one at a time. If you are still struggling, don't hesitate to seek help from your teacher or classmates.

Similar threads

  • Programming and Computer Science
Replies
4
Views
881
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
421
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top