Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. # DUE TUESDAY 24th 11:59PM******
  2. # Alec Greenaway -- CS 161
  3. # Input = allow user to type in an input n, that is a positive integer
  4. # Output = 1-2+3-4+5-6+7-8+9-10.......+/- n
  5.  
  6. # Variables:
  7. negNum = 0
  8. posNum = 0
  9. numList = []
  10. counterList = []
  11. counter = 1
  12. n = float(input("Enter a positive integer"))
  13. if (n < 0):
  14.     exit("Positive vibes only")
  15. lastNum = n
  16. finalString = ''
  17.  
  18. # creates a list of all the integer leading up to the chosen integer
  19.  
  20. while (counter <= lastNum):
  21.     numList.append(counter)
  22.     counter = counter + 1
  23.  
  24. # Makes even numbers negative, and keeps odd ones postive, and then calculates the sums of each:
  25. # Also build a string list that adds the correct operator to the end of number for printing purposes
  26. for num in range(0, len(numList)):
  27.     if (numList[num] % 2 == 0):
  28.         negNum += numList[num] * (-1)
  29.         finalString += str(numList[num]) + "+"
  30.     else:
  31.         posNum += numList[num]
  32.         finalString += str(numList[num]) + "-"
  33.  
  34. # Takes off un-needed operator at the end
  35. finalString = finalString[:-1]
  36.  
  37. # Prints a string of all the values up to n
  38. # Prints grand total of pos/neg numbers up to n
  39. print(finalString)
  40. print("Final Sum:", negNum + posNum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement