|
@@ -0,0 +1,32 @@
|
|
|
|
+#Program to find sum between 1 and certain number
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#Import sys module for verifying user input
|
|
|
|
+import sys
|
|
|
|
+
|
|
|
|
+#Ask for user input to enter a number
|
|
|
|
+userInput = input("Enter a number: ")
|
|
|
|
+
|
|
|
|
+#If user's input is of float type.....
|
|
|
|
+if isinstance(userInput, float) == True:
|
|
|
|
+
|
|
|
|
+ #Exit program with the message "Decimals are not allowed. Only whole numbers are."
|
|
|
|
+ sys.exit("Decimals are not allowed. Only whole numbers are.")
|
|
|
|
+
|
|
|
|
+#If error occurs when converting the input to int type, exit program with the message "Your input is not a number."
|
|
|
|
+try:
|
|
|
|
+ userNumber = int(userInput)
|
|
|
|
+except:
|
|
|
|
+ sys.exit("Your input is not a number.")
|
|
|
|
+
|
|
|
|
+#Create a variable named "userSum", and assign a value of 0
|
|
|
|
+userSum=0
|
|
|
|
+
|
|
|
|
+#Create for loop ranging from 1 to userNumber (variable declared above) + 1, and inside the for loop, ...
|
|
|
|
+for i in range(1,userNumber+1):
|
|
|
|
+
|
|
|
|
+ #Keep on adding loop variable to "userSum"
|
|
|
|
+ userSum += i
|
|
|
|
+
|
|
|
|
+#Display message showing the sum of numbers between 1 and "userInput"
|
|
|
|
+print("The sum of numbers from 1 to %s, is %d" %(userInput, userSum))
|