Contents
- sum5_advanced.py
- sum5_constant.py
- sum5_no_loop.py
- sum5.py
sum5_advanced.py 1/4
[top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111
print("This program will add up 5 numbers given by the user.")
print()
yourSum = 0
for whichNum in range(5):
yourNum = float(input("Enter number " + str(whichNum+1) + ": "))
yourSum = yourSum + yourNum # alternatively, yourSum += yourNum
#print("Checking our subtotal:", yourSum)
print("Your total is", yourSum)
sum5_constant.py 2/4
[top][prev][next]
# This program adds up numbers from the user.
# By CS111
NUMBER_OF_NUMBERS = 7
print("This program will add up", NUMBER_OF_NUMBERS, "numbers given by the user.")
print()
total = 0
for x in range(NUMBER_OF_NUMBERS):
# ask the user for a number
userInput = float(input("Pick one number: "))
# add the number to the previous number
total = total + userInput
print("The total of your numbers is", total)
sum5_no_loop.py 3/4
[top][prev][next]
# Representative of the way to implement sum5.py before today's lecture.
# What happens if I asked you to add up 15 numbers...
# Or 100 numbers intead? What would writing that code be like?
print("This program will total 5 numbers entered by you!")
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
num3 = input("Enter the third number: ")
num4 = input("Enter the fourth number: ")
num5 = input("Enter the fifth number: ")
total = num1 + num2 + num3 + num4 + num5
print("The total of these numbers is", total)
sum5.py 4/4
[top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111
NUM_TIMES = 5
print("This program will add up 5 numbers given by the user.")
print()
total = 0
# repeat 5 times:
for x in range(5):
# get user input
num = float(input("Enter a number: "))
# add the user's input to the running total
total = total + num
#print("Your subtotal is", total) <-- just for debugging
print("Your total is", total)
Generated by GNU Enscript 1.6.6.