Example #1
0
abspath = Path(__file__)
toolspath = abspath.parents[3]

sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger

kPaConversionRatioTable = {
    "ppsi": 0.145037738,
    "mmhg": 7.50061683,
    "atm": 0.00986923267
}


def kPaConverter(kPa, targetUnit):
    try:
        ratio = kPaConversionRatioTable[targetUnit]
    except KeyError:
        print("Unknown target unit.")
        return
    else:
        return kPa * ratio


#get the kPa value:
print("Give me the pressure in kPa (non-negative integer):")
kPa = GetNonNegativeInteger()

for key in kPaConversionRatioTable:
    print("%d kPa is %f %s." % (kPa, kPaConverter(kPa, key), key))
Example #2
0
"""
"""
SOLUTION:
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]

sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger


def ConvertToCm(ft, inch):
    feetToCmRatio = 30.48
    inchesToCmRatio = 2.54
    return (feet * feetToCmRatio + inches * inchesToCmRatio)


#Get the feet:
print("Lets set the feet:")
feet = GetNonNegativeInteger()

#Get the inches:
print("Let's set the inches:")
inches = GetNonNegativeInteger()

print("%d ft and %d in is %f cm." % (feet, inches, ConvertToCm(feet, inches)))
"""
Write a Python program to get the n (non-negative integer) copies of the
first 2 characters of a given string.
Return the n copies of the whole string if the length is less than 2.
"""
"""
SOLUTION:
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]
sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger


def multiplyStringNTimes(myStr, myInt):
    return myInt * myStr


myInt = GetNonNegativeInteger()

myString = input('Give a string:')

print(multiplyStringNTimes(myString[:2], myInt))
"""
Write a Python program to compute the future value of a specified principal amount,
rate of interest, and a number of years.
Test Data : amt = 10000, int = 3.5, years = 7
Expected Output : 12722.79
"""
"""
SOLUTION:
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]
sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeFloat, GetNonNegativeInteger


def calculateFutureValue(origAmount, intRate, numberOfYears):
    factor = 1 + (intRate / 100)
    return origAmount * ((factor)**numberOfYears)


amt = GetNonNegativeFloat()
rate = GetNonNegativeFloat()
yrs = GetNonNegativeInteger()

print(calculateFutureValue(amt, rate, yrs))
"""
Write a Python program to calculate the sum of the digits in an integer.
"""
"""
SOLUTION:
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]

sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger


def sumOfDigits(num):
    num_str = str(num)
    result = 0
    for digit in num_str:
        result += int(digit)
    return result


myNum = GetNonNegativeInteger()

print("Sum of digits in %d is %d." % (myNum, sumOfDigits(myNum)))
Example #6
0
import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]

sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger

feetConversionRatioTable = {"inches": 12, "yards": 1 / 3, "miles": 1 / 5280}


def feetConverter(feet, targetUnit):
    try:
        ratio = feetConversionRatioTable[targetUnit]
    except KeyError:
        print("Unknown target unit.")
        return
    else:
        return feet * ratio


#Get the feet:
print("Let's set the distance (in feet):")
feet = GetNonNegativeInteger()

print("%d feet is %s inches." % (feet, feetConverter(feet, "inches")))
print("%d feet is %s yards." % (feet, feetConverter(feet, "yards")))
print("%d feet is %s miles." % (feet, feetConverter(feet, "miles")))
Example #7
0
"""
Write a Python program to display your details like name, age, address in three different lines.
"""
"""
SOLUTION:
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]
sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger


def printDetails(name, address, age):
    print("Name: %s,\nAddress: %s,\nAge: %d" % (name, address, age))


#get name
myName = input("Give me yor name:")
#get address
myAddress = input("Give me your address:")
#get age
print("Let's set your age.")
myAge = GetNonNegativeInteger()

print(printDetails(myName, myAddress, myAge))
Example #8
0
Write a Python program to calculate body mass index.
"""
"""
SOLUTION:
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]

sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger


def calculateBMI(height, mass):
    height_meters = height / 100
    return mass / (height_meters**2)


#get the height
print("Give me your height (in cm):")
height = GetNonNegativeInteger()

#get the mass
print("Give me your mass (in kg):")
mass = GetNonNegativeInteger()

print("BMI index: %f" % calculateBMI(height, mass))
"""

import sys
from pathlib import Path

abspath = Path(__file__)
toolspath = abspath.parents[3]

sys.path.append(str(toolspath / 'tools'))

from GetFromUser import GetNonNegativeInteger


def secondsToDaysHoursMinutesSeconds(scnds):
    seconds = scnds
    days = seconds // (24 * 60 * 60)
    seconds = seconds % (24 * 60 * 60)
    hours = seconds // (60 * 60)
    seconds = seconds % (60 * 60)
    minutes = seconds // 60
    seconds = seconds % 60
    return days, hours, minutes, seconds


#get the number of seconds:
print("Let's set the number of seconds:")
seconds = GetNonNegativeInteger()

print("%d day(s), %d hour(s), %d minute(s) and %d second(s)" %
      secondsToDaysHoursMinutesSeconds(seconds))