New Codes Estrutura Sequencial

exercise 1

# coding=utf-8
#1.1
 
def olaMundo():
    print "olá mundo"
 
if __name__ == '__main__': 
    olaMundo()

exercise 2

# coding=utf-8
#1.2
 
def askNumber():
    number = float(raw_input("input a number -> "))
    return number
 
def printNumber(number):
    print "the number inserted was %.2f" % number
 
if __name__ == '__main__':
    printNumber(askNumber())

exercise 3

# coding=utf-8
#1.3
 
def askNumbers(qtyNumbers):
    numberList = []
    for i in range(1,qtyNumbers +1):
        numberList.append(float(raw_input("input a number -> ")))
 
    return numberList
 
def sumNumbersFromList(numberList):
    sumNumbers = 0
    for numbers in numberList:
        sumNumbers = sumNumbers + numbers
    return sumNumbers
 
def printResult(numberList, sumNumbers):
    print "\nnumbers:"
    print numberList
    print "the result is: %.2f" % float(sumNumbers)
 
def questionsAndInteraction():
    qtyNumbers = int(raw_input("how many numbers do you want to sum? --> "))
    numberList = askNumbers(qtyNumbers)
    printResult(numberList, sumNumbersFromList(numberList))
 
if __name__ == "__main__":
    questionsAndInteraction()

exercise 4

# coding=utf-8
#1.4
 
def askGrades(numberGrades):
    gradeList = []
    for i in range(1,numberGrades +1):
        gradeList.append(float(raw_input("input a grade -> ")))
 
    return gradeList
 
def avgGradesFromList(gradeList):
    sumGrades = 0
    for grades in gradeList:
        sumGrades = sumGrades + grades
    return float(sumGrades)/len(gradeList)
 
def printResult(gradeList, avgGrades):
    print "\ngrades:"
    print gradeList
    print "the avg grades is: %.2f" % float(avgGrades)
 
def questionsAndInteraction():
    qtyNumbers = int(raw_input("how many grades do you want to input? --> "))
    gradeList = askGrades(qtyNumbers)
    printResult(gradeList, avgGradesFromList(gradeList))
 
if __name__ == '__main__':
    questionsAndInteraction()

exercise 5

# coding=utf-8
#1.5
 
def m2cm(metres):
    return float(metres*100)
 
def questionAndInteraction():
    metres = float(raw_input("input a metre value to convert --> "))
    print "%.2f m --> %.2f cm" % (metres, m2cm(metres))
 
if __name__ == '__main__':
    questionAndInteraction()

exercise 6

# coding=utf-8
#1.6
 
def circleArea(radius):
    return 3.14*radius**2
 
def questionAndInteraction():
    radius = float(raw_input("insert radius value --> "))
    print "the circle area is --> %.2f u2" % circleArea(radius)
 
if __name__ == '__main__':
    questionAndInteraction()

exercise 7

# coding=utf-8
#1.7
 
def squareArea(side):
    return side**2
 
def questionAndInteraction():
    side = float(raw_input("input the square side --> "))
    print "the square area is %.2f u2 \
           \nthe double of square area is %.2f u2" % (squareArea(side), 2*squareArea(side))
 
if __name__ == '__main__':
    questionAndInteraction()

exercise 8

# coding=utf-8
#1.8
 
def workHourDic(hoursWorked, hourValue):
    workDic = {}
    workDic["hoursWork"] = hoursWorked
    workDic["hourValue"] = hourValue 
    return workDic
 
def monthSalary(workDic):
    return workDic["hoursWork"]*workDic["hourValue"]
 
def askAndInteraction():
    hoursWorked = float(raw_input("input qty hours worked --> "))
    hourValue = float(raw_input("input the work hour value --> "))
    print "Your salary is $ %.2f" % monthSalary(workHourDic(hoursWorked, hourValue))
 
if __name__ == '__main__':
    askAndInteraction()

exercise 9

# coding=utf-8
#1.9
 
def farenheint2Celcius(tempF):
    return (5*(tempF-32))/9
 
def askAndInteraction():
    tempF = float(raw_input("input a temperature in farenheit --> "))
    print "%.2f F --> %.2f C" % (tempF, farenheint2Celcius(tempF))
 
if __name__ == '__main__':
    askAndInteraction()

exercise 10

# coding=utf-8
#1.10
 
def celcius2Farenheint(tempC):
    return (9*tempC)/5 + 32
 
def askAndInteraction():
    tempC = float(raw_input("input a temperature in celsius --> "))
    print "%.2f C --> %.2f F" % (tempC, farenheint2Celcius(tempC))
 
if __name__ == '__main__':
    askAndInteraction()

exercise 11

# coding=utf-8
#1.11
 
def generateIntegerList(qtyNumbers):
    integerList = []
    for i in range(0,qtyNumbers):
        integerList.append(int(raw_input("input a integer number -> ")))
    return integerList
 
def generateFloatList(qtyNumbers):
    floatList = []
    for i in range(0,qtyNumbers):
        floatList.append(float(raw_input("input a float number -> ")))
    return floatList
 
def printOperations(integerList, floatList):
    result1 = integerList[0]*2*((integerList[1])/2)
    result2 = integerList[0]*2+floatList[0]
    result3 = floatList[0]**3
    print "1 --> %.2f" % result1
    print "2 --> %.2f" % result2
    print "3 --> %.2f" % result3
 
if __name__ == '__main__':
    integerList = generateIntegerList(2)
    floatList = generateFloatList(1)    
    printOperations(integerList,floatList)

exercise 12

# coding=utf-8
#1.12
 
def idealWeight(h):
    return (72.7*h)-58
 
def askAndInteraction():
    h = float(raw_input("input your height (metres)--> "))
    return idealWeight(h)
 
if __name__ == '__main__':
    print "ideal weight --> %.2f" % askAndInteraction()

exercise 13

# coding=utf-8
#1.13
 
def idealWeight(h, s):
    s = s.upper().strip()
    if s == "M":
        return (72.7*h)-58
    else:
        return (62.1*h)-44.7
 
def askAndInteraction():
    h = float(raw_input("input your height (metres)--> "))
    sex = raw_input("input your sex -> ")
    weight = float(raw_input("input your weight (kg)-->"))
    idealWeightV = idealWeight(h, sex)
    print "ideal weight --> %.2f kg" % idealWeightV
    if weight == idealWeight: 
        print "you are with a ideal weight!"
    elif weight < idealWeightV:
        print "you are bellow your ideal weight"
    elif weight > idealWeightV:
        print "you are above your ideal weight"
 
if __name__ == '__main__':
    askAndInteraction()

exercise 14

# coding=utf-8
#1.14
 
def fishWeightDiff(fishWeight):
    return fishWeight-50
 
def tax(weightDiff):
    if weightDiff > 0:
        return [weightDiff, weightDiff*4]
    else:
        return [0,0]
 
def questionsAndInteraction():
    fishWeight = float(raw_input("input the fish weight (kg) --> "))
    print "you had %.2f wight diff and have to pay $ %.2f taxs" % (tax(fishWeightDiff(fishWeight))[0],
                                            tax(fishWeightDiff(fishWeight))[1])
 
if __name__ == '__main__':
    questionsAndInteraction()

exercise 15

# coding=utf-8
#1.
 
if __name__ == '__main__':
pass

exercise 16

# coding=utf-8
#1.16
 
def paintQty(area):
    return area/3
 
def canQty(paintQty):
    if paintQty < 18:
        return 1
    else:
        return paintQty/18
 
def totalPrice(canQty, price):    
    return canQty*price
 
def questionsAndInteraction():
    areaToPaint = float(raw_input("input the value of are to paint m2-> "))
    paintQuantity = float(paintQty(areaToPaint))
    canQuantity = canQty(paintQuantity)
    total_price = float(totalPrice(canQuantity,25))
    print "CanQty - %d\nTotal Price - $ %.2f" % (canQuantity,total_price)
 
if __name__ == '__main__':
    questionsAndInteraction()

exercise 17

# coding=utf-8
#1.17
 
import math
 
def paintQty(area):
    return area/6
 
def bestCanQty(paintQty):
    countSmallCan = 0
    countBigCan = 0
    while paintQty > 0:
        if countSmallCan > 3:
            countBigCan = countBigCan + 1
            countSmallCan = 0
            paintQty = (3*3.6 + paintQty) - 18
        else:
            paintQty = paintQty - 3.6
            countSmallCan = countSmallCan + 1
 
    return {"countSmallCan":countSmallCan, "countBigCan":countBigCan}    
 
def bigCanQty(paintQty):
    if paintQty/18 < 0:
        return 1
    else:
        return math.floor((paintQty/18) + 1)
 
def smallCanQty(paintQty):
    if paintQty/3.6 < 0:
        return 1
    else:
        return math.floor((paintQty/3.6)+1)
 
def totalPrice(canQty, canType):
    if canType == 1:
        return canQty*25
    else:
        return canQty*80
 
def totalBestPrice(bestDicQty):
    return (bestDicQty["countSmallCan"]*25) + (bestDicQty["countBigCan"]* 80)
 
def questionsAndInteraction():
    areaToPaint = float(raw_input("input the value of are to paint m2-> "))
    paintQuantity = float(paintQty(areaToPaint))
    smallCanQuantity = smallCanQty(paintQuantity)
    bigCanQuantity = bigCanQty(paintQuantity)
    bestCanQuantity = bestCanQty(paintQuantity)
    #total_price = float(totalPrice(canQuantity,25))
    print "\nBigCanQty - %d\nTotal Price - $ %.2f\n" % (bigCanQuantity,totalPrice(bigCanQuantity,2))
    print "SmallCanQty - %d\nTotal Price - $ %.2f\n" % (bigCanQuantity,totalPrice(bigCanQuantity,1))
    print "Best Price!!\nSmall - %d\nBig - %d\nTotal Price - $ %.2f\n" % (bestCanQuantity["countSmallCan"],
                                                     bestCanQuantity["countBigCan"],
                                                     totalBestPrice(bestCanQuantity))
 
if __name__ == '__main__':
    questionsAndInteraction()

voltar

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.