Contents

  1. function_example.py
  2. print_examples.py
  3. sum5_advanced.py
  4. sum5_constant.py
  5. sum5_no_loop.py
  6. sum5.py

function_example.py 1/6

[
top][prev][next]
# Examples using built-in functions
# Sara Sprenkle

x = 6.817454321
#x = 5.6512542

print("We start with x having value", x)

# Call the function round with input x
# Then, save output of function call in variable roundedXInt
roundedXInt = round(x)
print("x rounded to the nearest int:", roundedXInt)

roundedXTenth = round(x, 1)
print("x rounded to the nearest tenth:", roundedXTenth)

a = round(x, 2)
print("x rounded to the nearest hundredth:", a)
# demonstrating that the name doesn't matter,
# but good names make the code easier to understand

roundx = round(x, 3)
print("x rounded to the nearest thousandth:", roundx)

print(round(x, 4))

print("-"*40)
print("x is of", type(x))

print_examples.py 2/6

[
top][prev][next]
# Examples calling the print function
# Sara Sprenkle for CSCI111

print("Hi", "there", "class", sep='; ')
# By default end is "\n" --> called "the new line character"
# means, put the next displayed text on the next line.
print("Put on same", end='')
print("line")

# make end=" " (a space) instead:
print("Put on same", end=' ')
print("line")

sum5_advanced.py 3/6

[
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 4/6

[
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 5/6

[
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 6/6

[
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()

total = 0

for x in range(5):
    # ask the user for a number
    userInput = float(input("Pick one number: "))
    # add the number to the previous number
    total = total + userInput
    # Alternatively, write above as total += userInput
    
print("The total of your numbers is", total)

Generated by GNU Enscript 1.6.6.