Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Quizzma Latest Articles

4.8.4 Better Sum CodeHS Answers

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 Team

Quizzma Team

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.

Related Posts

Leave a comment