Python (if installed properly) always tells the terminal where it is located, so, relative to Python, the program doesn’t need to be in the same directory, or even referenced in relation to it.
*/ — set up Cygwin on the Dellosaurus to do the same thing, if possible? — /*
First program (in Xcode on mac):
returns this output in Python 3.3.4 in Terminal:
because the program is processed in a linear order, we can change the value of “sentence” at any point after where it was initially set
but if you don’t redefine the other variables, it returns:
redefining the variables for the second sentence:
you get the correct, expected output, and the correct number count:
I wonder if there is a more concise way to do this, or maybe I’m doing it incorrectly?
the for loop
unlike other programming languages, Python uses indentation to define the scope of the loop (not {} like some, etc.)
to start counting, we have to set the total to 0 so that we can mark it as an integer, and have a starting point
the loop runs in the interactive mode in Python:
# -- for loop for number in [1, 2, 3]: number *= 2 print(number) # -- processing 1 number = 1 number *= 2 print(number) # -- processing 2 number = 2 number *= 2 print(number) # -- processing 3 number = 3 number *= 2 print(number)
in class programs:
sentence.py
#this is a note not seen by Python # -- first test # print(" ") # print("hello world!") # -- doing it the long way (sentence 1) # print(" ") # print("Here are all the words:", "B-f: I'm in graduate school".split()) # print("This sentence has", len("B-f: I'm in graduate school".split()), "words") # print(" ") # print("If we remove the identifier at the beginning, then...") # print(" ") # print("Here are all the words:", "B-f: I'm in graduate school".split()[1:]) # print("This sentence has", len("B-f: I'm in graduate school".split()[1:]), "words") # print(" ") # print(" ", "assigning variables to the things that we've just input ") # print(" ", "it's a good idea to give a sensible name to your variable") # -- adding variables (sentence 1) print(" ") sentence = "B-f: I'm in graduate school" words = sentence.split() onlywords = words[1:] gender = words[0][2] speakerID = words[0][0] numberwords = len(onlywords) # print("all word-like things:", words) # print(" ") # print("the words", onlywords, "were spoken by a", gender, "person") print(" ", "for the sentence,", sentence) print(" ") print(" ", "number of words:", numberwords) print(" ", "words are:", onlywords) print(" ", "speaker ID:", speakerID) print(" ", "speaker is:", gender) # print(" ") # print(" * --", "figure out how to print something based on the input", "-- *") # print(" * --", "like, return 'Female' for the 'gender' variable 'f'", "-- *") # print(" ", "") # -- looking at a second sentence (sentence 2): changing the value of "sentence", and re-running everything # -- taking the whole thing out of the print (after the fact) print(" ") sentence = "B-f: at OSU" words = sentence.split() # only need to redefine the "words" variable # -- don't need to redefine the other variables print(" ", "for the sentence,", sentence) print(" ") print(" ", "number of words:", numberwords) print(" ", "words are:", onlywords) print(" ", "speaker ID:", speakerID) print(" ", "speaker is:", gender) print(" ")
# -- initialization of tracking variables totalWordsSpoken = 0 totalUtterances = 0 # -- adding variables (sentence 1) print(" ") sentence = "B-f: I'm in graduate school" words = sentence.split() onlywords = words[1:] gender = words[0][2] speakerID = words[0][0] numberwords = len(onlywords) totalWordsSpoken += len(onlywords) totalUtterances += 1 print(" ", "sentence 1") print(" ", "number of words:", numberwords) print(" ", "words are:", onlywords) print(" ", "speaker ID:", speakerID) print(" ", "speaker is:", gender) # -- sentence 2 print(" ") sentence = "B-f: at OSU" words = sentence.split() # define these variables again onlywords = words[1:] gender = words[0][2] speakerID = words[0][0] numberwords = len(onlywords) totalWordsSpoken += len(onlywords) totalUtterances += 1 print(" ", "sentence 2") print(" ", "number of words:", numberwords) print(" ", "words are:", onlywords) print(" ", "speaker ID:", speakerID) print(" ", "speaker is:", gender) # -- final output print(" ") print(" ", "total number of words:", totalWordsSpoken) print(" ", "total number of utterances:", totalUtterances) print(" ", "the average number of words per utterance was :", totalWordsSpoken / totalUtterances) print(" ")