#simple function #creating a function def cube(): number = int(input("Enter a number ")) myCube=number * number * number print ("the cube is", myCube) def main(): cube()#calling it in main main()
Modified cube functions
#simple functions def getInput(): number = int(input("Enter a number ")) return number def cube(number): myCube = number * number * number return myCube def printCube(myCube): print ("The cube is",myCube) def main(): number=getInput() myCube=cube(number) printCube(myCube) main()
Kilometers conversion program
#conversion to kilometers #get the input in miles #calculation miles * 1.609 #print out results def getMiles(): miles=float(input("enter the miles ")) return miles def convert(m): kilometers=m * 1.609 return kilometers def printKilometers(k): print (k) def main(): mls = getMiles() ks = convert(mls) printKilometers(ks) main()
Word length
main : getTotalWordLength() TotalWordLength: getSentence() splitSentence() calcAverage() printAverage()
#Figure out the average length of a word in a sentence #Get the sentence #split sentence into words #loop through the words and get the lengths #total the lengths #divide the total length by the number of words #print the average def getSentence(): sentence=input("Enter a sentence ") return sentence def splitSentence(): mySentence=getSentence() words=mySentence.split() return words def getTotalWordLength(): words=splitSentence() counter=0 for i in range(0, len(words)): counter += len(words[i]) #counter = counter + len(words[i]) average=calcAverageWordLength(counter, len(words)) return average def calcAverageWordLength(total, sizeOfList): average = total/sizeOfList return average def printAverage(): print("The average word length in your sentence is",getTotalWordLength()) def test(): #print(getSentence()) #sentenceWords=splitSentence() #for i in range(0, len(sentenceWords)): #print(sentenceWords[i]) #print(getTotalWordLength()) print(getTotalWordLength()) #test() def main(): printAverage() main()
No comments:
Post a Comment