Here is the code for my question. I was trying to translate a pseudocode into python. The function, ExCamel, forms a return string, OutString, from a given string,InString,by:
1:Separating the original words(a word is assumed to start with a capital letter)
2:Converting all characters to lower case.
InString=str(input("Enter a statement: "))
def ExCamel(InString):
NextChar=chr()
OutString=str()
n=int()
OutString=""
return OutString
for n in range(1,len(ExCamel(Instring))):
NextChar=InString[n:n+1]
if NextChar==upper(NextChar):
if n>1:
OutString=OutString+""
NextChar=lower(NextChar)
OutString=OutString+NextChar
But it gives an output of:
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/George/TrynaCreate.py", line 7, in <module>
for n in range(1,len(ExCamel(Instring))):
NameError: name 'Instring' is not defined
This just seems like a silly mistake which happens with most of us:
you have defined
InString
but you are using In's'tring
Hope that helps :)
Related
I have built a code for a codewars problem. I think it is correct but it shows me an error I don't understand.
Can you tell me what am I doing wrong?
import math
def waterbombs(fire, w):
s=""
countx=0
for i in fire:
if i=="x":
countx+=1
elif i=="Y":
countx=0
return sum(math.ceil(countx/w))
waterbombs("xxYxx", 3)
This is the error:
Traceback (most recent call last):
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 16, in <module>
waterbombs("xxYxx", 3)
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 13, in waterbombs
return sum(math.ceil(countx/w))
TypeError: 'int' object is not iterable
[Finished in 0.2s]
Why are you doing sum(math.ceil(countx/w)) ?
What is the objective of the sum method here, since there is only value returned by math.ceil ?
The sum would throw that error if you pass a single value to it. You're supposed to pass a list of values of the sum method.
For eg: sum(5) would give you the same error you see above, but sum([5]) would return you 5.
I have this for-loop:
for i in range(1000000000, 1000000030):
foo(i)
When I execute it this error is given:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 11, in <module>
for i in range(1000000000, 10000000030):
OverflowError: range() result has too many items.
As far as I know, this range-object should have exactly 30 elements...
Where is the problem?
Edit:
I have removed the extra zero, now I get this:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 12, in <module>
factorizeInefficient(i)
MemoryError
Edit 2:
def factorizeInefficient(n):
teiler = list()
for i in range(n):
if i != 0:
if (n%i)==0:
teiler.append(i)
print teiler
Just found the solution myself: There is a range(n) object in this as well and this causes the memory Error...
An extra question: How did you guys know this was python 2? (Btw you were right...)
Copy/pasting the range() part of your code:
>>> len(range(1000000000, 10000000030))
9000000030
So there are actually about 9 billion elements in the range. Your first argument is presumably missing a zero, or second argument has a zero too many ;-)
count your zeros once again ;) I'd say it's one too much.
Can someone please explain to me why I keep getting this error: TypeError: get_n_nouns() takes 1 positional argument but 2 were given.
I have already had a look at where my problem may be by looking at a similar question (Link) But I have adapted my code going along with the answer and yet I end up with the above error.
Here is the error in full:
Traceback (most recent call last):
File "C:/Users/...../Downloads/Comp4.1/trialTo3.py", line 21, in <module>
app.createPhrases()
File "C:/Users/...../Downloads/Comp4.1/trialTo3.py", line 15, in createPhrases
words = self.get_n_nouns(1)
TypeError: get_n_nouns() takes 1 positional argument but 2 were given
Here is the code:
import csv
class apps():
def get_n_nouns(n):
"""
Returns the n most common nouns
"""
with open("setPhrases.txt") as in_file:
reader = csv.reader(in_file)
data = [[row[0], int(row[1])] for row in list(reader)]
return sorted(data, key=lambda x: -x[1])[:n]
def createPhrases(self):
words = self.get_n_nouns(1)
for word, count in words:
print("{}: {}".format(word, count))
app = apps()
app.createPhrases()
Can someone please explain to me where I am going wrong? Any help is much appreciated.
Ok so I found out where the error was. Kind of a rookie error.
This:
def get_n_nouns(n):
Needed to be written as this:
def get_n_nouns(self, n):
I had forgot to add the self part to it. That is why I kept getting that error message.
We just learned for loops in class for about five minutes and we were already given a lab. I am trying but still not getting what I need to get. What I am trying to do is take a list of integers, and then only take the odd integers and add them up and then return them so if the list of integers was [3,2,4,7,2,4,1,3,2] the returned value would be 14
def f(ls):
ct=0
for x in (f(ls)):
if x%2==1:
ct+=x
return(ct)
print(f[2,5,4,6,7,8,2])
the error code reads
Traceback (most recent call last):
File "C:/Users/Ian/Documents/Python/Labs/lab8.py", line 10, in <module>
print(f[2,5,4,6,7,8,2])
TypeError: 'function' object is not subscriptable
Just a couple of minor mistakes:
def f(ls):
ct = 0
for x in ls:
# ^ Do not call the method, but just parse through the list
if x % 2 == 1:
ct += x
return(ct)
# ^ ^ parenthesis are not necessary
print(f([2,5,4,6,7,8,2]))
# ^ ^ Missing paranthesis
You're missing the parenthesis in the function call
print(f([2,5,4,6,7,8,2]))
rather than
print(f[2,5,4,6,7,8,2])
Each line represents a single student and consists of a student number, a name, a section code and a midterm grade, all separated by whitespace.
The first parameter is already done and the file is open and
The second parameter is a section code
this is the link http://www.cdf.toronto.edu/~csc108h/fall/exercises/e3/grade_file.txt
My code:
def average_by_section(the_file, section_code):
'''(io.TextIOWrapper, str) -> float
Return the average midtermmark for all students in that section
'''
score = 0
n = 0
for element in the_file:
line = element.split()
if section_code == line[-2]:
mark = mark + float(line[-1])
n += 1
lecture_avg = mark / n
return lecture_avg
I'm getting an index out of range. Is this correct? Or am I just opening up the wrong file?
can someone test this code and download that file? I'm pretty sure it should work, but not for me.
Well, you can troubleshoot the index out of range error with a print line or print(line) to explore the number of items in "line" (i.e. the effect of split()). I'd suggest looking closer at your split() statement...
It looks like you are omitting parts of your code where you define some of those variables (section_code, mark, etc.), but adjusting for some of those things seems to work properly. Assuming that the error you got was IndexError: list index out of range, that happens when you try to access an element of a list by index where that index doesn't exist. For instance:
>>> l = ['one']
>>> l[0]
'one'
>>> l[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> l[-1]
'one'
>>> l[-2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Therefore in your code, you will get that error if line is ever fewer than two items. I would check and see what you are actually getting for line to make sure it is what you expect.