archivo = open(“carros.txt”, “r”)
lineas = archivo.readlines()
#def primero:
y = 0
contcity = 0
for i in lineas:
x = (i[52:54])
if (x != “”):
x = float(x)
y = y + x
contcity = contcity + 1
city = y//contcity
print(‘The average gas mileage in city is: ‘,city)
p = 0
conthigh = 0
for i in lineas:
z = (i[55:57])
if (z != “”):
z = float(z)
p = p + z
conthigh = conthigh + 1
high = p // conthigh
print(‘The average gas mileage in highway is: ‘,high)
f = 0
contmind = 0
for i in lineas:
a = (i[42:46])
if (a != “”):
a = float(a)
f = f + a
contmind = contmind + 1
mind = y//contmind
print(‘The average mind-range price is: ‘,mind)
#52
]]>To learn how to open and read files i had to read a lot of times the same thing of the “How to think like a computer scientist”
There are special words to do this how ever the esscentials does not need you to import any module which is good for us.
First of all we have to tell python we want to open a file, to do this we must type set a variable and set the open to it like this:
variable = open(“name of the file.txt”, “r”)
It is very important that we put our paraeters in this case within quot marks, as you see up you have to type an ‘r’ after the name of the text which indicates that is reading.
Now we have it open we have to read it.
To read the text we can choose between read line by line orto read all of them.
1 line at a time: variable = previous_variable.readline()
all of them: variable = previous_variable.readlines()
From here you just haveto get creative and do the stuff you want. And just dont remember that always at the end you must close your text with:
variable.close
An example:
archivo = open(‘bananas.txt’, ‘r’)
one = archivo.readline()
print (one)
close.archivo
Hope you enjoy it!
]]>
Here is my code:
]]>But there are some numbers that havent been able to prove this theory, i even read that ones, scientifics let a computer programm running for three years in search for its palyndrome, eventually they stopped the programm and found a number of one million digits which is a lot, but never the palyndrome.
I get confused a lot with variables but at the end Ken helped me to fix the last problems, here is my code: https://github.com/sabaldaba/TC101/blob/master/wsq11_soy:196
Hope you like it!
]]>A recursion allow a function to be written inside itself so it can run more than one time. The function would be running itself.
A lot of information about recursion states that knowing how to create a program that gets you the factorial number is an awesome begining to understand recursion.
The code would be like this.
def factorial(a):
if a == 1:
return 1
return (a*factorial(a-1))
We can see how at the last line the programm compute the function inside the function. The programm would do something like
5! = 4 * 3 * 2 * 1
and this will make it faster and more efficient
]]>if variable < 200:
if variable == 150:
do something
elif variable == 100:
do something
else:
do something
else variable > 200:
do something
We can see how in here we evalate if our variable was in range below 200 and then if it had exactly the value of 150 and then of 100, if non of there were true we also state something.
This is what nested conditionals work for.
]]>
I struggled with the file question since i dont know how to use properly the .split() very well but still i did my best effort.
Here is my code of the first question: https://github.com/sabaldaba/TC101/blob/master/Question1-quiz
And the second one: https://github.com/sabaldaba/TC101/blob/master/question2-quiz11
]]>