Thursday, October 19, 2017

String and file stuff

#List a collection of values under one name

def main():
    dayNames=["Sun","Mon","Tue", "Wed", "Thu", "Fri", "Sat"]
    day = int(input(" Enter a number between 1 and 7 "))
    dayOfWeek = dayNames[day-1]
    print(dayOfWeek)

main()
    
    

Broken into functions

#Same program, but methods
#Define array
#get input
#Do the conversion number to word
#output

def setDays():
    dayNames=["Sun","Mon","Tue",
              "Wed", "Thu", "Fri", "Sat"]
    return dayNames

def getDay():
    day = int(input(" Enter a number between 1 and 7 "))
    return day

def getDayName(dayNumber):
    days=setDays()
    dayOfWeek = days[dayNumber-1]
    return dayOfWeek

def printDay(dayName):
    print (dayName)

def main():
    day = getDay()
    weekDay = getDayName(day)
    printDay(weekDay)

main()
    

File processing

#Files

def main():
    fileName=input("Enter File Name")
    inFile = open(fileName, "a")
    content = input("Write Something ")
    print(content, file=inFile)
    inFile.close()

    print("********************")
    inFile = open(fileName,"r")
    text=inFile.read()
    print (text)
    inFile.close()

main()

More Strings

#strings

def main():
    sentence=input("Enter a sentence  ")
    #print(sentence[0])
    #for i in range(0, len(sentence)):
       # print(ord(sentence[i]))
    #print(chr(962))
    #words=sentence.split(",")
    #for str in words:
        #print(str.upper())
    middle=sentence[5:10]
    print(middle)
    

main()

No comments:

Post a Comment