Discover answers to your questions with Westonci.ca, the leading Q&A platform that connects you with knowledgeable experts. Get detailed and precise answers to your questions from a dedicated community of experts on our Q&A platform. Get quick and reliable solutions to your questions from a community of experienced experts on our platform.

4.8.4 Better Sum
Write a program that asks the user for two numbers. Using a for loop, add all of the numbers from the first to the second.


For example if the first number is 6 and the second number is 8 the result is 21 (6 + 7 + 8).


Print out the results when you are finished.

write in python


Sagot :

import sys

x = input("enter number 1: ") #ask for user input

y = input("enter number 2: ")

output = "" #the output string

try: #try integering them (this also has error message and will check if input is valid)

x = int(x)

except:

print("error! number 1 was not a number :(")

sys.exit() #leave

try:

y = int(y)

except:

print("error! number 2 was not a number :(")

sys.exit()

if x == y: #check they are not the same

print("both numbers are the same")

else: #do the thing

if y > x: #otherwise swap the order

for i in range (x, y + 1): #+1 for inclusive

output = (output + str(i) + " ")

else:

for i in range (y, x + 1):

output = (output + str(i) + " ")

print(output) #so it's all in one line

Thanks for stopping by. We strive to provide the best answers for all your questions. See you again soon. Thanks for using our service. We're always here to provide accurate and up-to-date answers to all your queries. Get the answers you need at Westonci.ca. Stay informed by returning for our latest expert advice.