4.8.4 Better Sum CodeHS Answers

We thoroughly check each answer to a question to provide you with the most correct answers. Found a mistake? Tell us about it through the REPORT button at the bottom of the page. Ctrl+F (Cmd+F) will help you a lot when searching through such a large set of questions.

The most common answer for the 4.8.4 Better Sum CodeHS is:

# Enter your code here
firstnum = int(input("What is your frist number?: "))
lastnum = int(input("What is your second number?: "))
sum = 0
for i in range(firstnum, lastnum + 1):
sum += i
print(sum)

It is almost correct, but it seems to lack proper indentation, which is essential in Python. Here’s the corrected version with proper indentation:

firstnum = int(input("What is your first number?: "))
lastnum = int(input("What is your second number?: "))
sum = 0

for i in range(firstnum, lastnum + 1):
    sum += i

print(sum)

This code will take two numbers as input from the user and calculate the sum of all the numbers from the first number to the second number, inclusive. The range(firstnum, lastnum + 1) function generates a sequence of numbers starting from firstnum and ending at lastnum. The loop then iterates through this range, adding each number to the sum variable. After the loop completes, the total sum is printed.

Just make sure that firstnum is less than or equal to lastnum. If firstnum is greater than lastnum, the range function will not generate any numbers, and the sum will be 0.

Was this helpful?

quizzma
Quizzma Team
+ posts

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.




Leave a Comment

Your email address will not be published. Required fields are marked *