Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # DUE TUESDAY 24th 11:59PM******
- # Alec Greenaway -- CS 161
- # Input = allow user to type in an input n, that is a positive integer
- # Output = 1-2+3-4+5-6+7-8+9-10.......+/- n
- # Variables:
- negNum = 0
- posNum = 0
- numList = []
- counterList = []
- counter = 1
- n = float(input("Enter a positive integer"))
- if (n < 0):
- exit("Positive vibes only")
- lastNum = n
- finalString = ''
- # creates a list of all the integer leading up to the chosen integer
- while (counter <= lastNum):
- numList.append(counter)
- counter = counter + 1
- # Makes even numbers negative, and keeps odd ones postive, and then calculates the sums of each:
- # Also build a string list that adds the correct operator to the end of number for printing purposes
- for num in range(0, len(numList)):
- if (numList[num] % 2 == 0):
- negNum += numList[num] * (-1)
- finalString += str(numList[num]) + "+"
- else:
- posNum += numList[num]
- finalString += str(numList[num]) + "-"
- # Takes off un-needed operator at the end
- finalString = finalString[:-1]
- # Prints a string of all the values up to n
- # Prints grand total of pos/neg numbers up to n
- print(finalString)
- print("Final Sum:", negNum + posNum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement