NameError: global name 'Solution' is not defined [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I am trying to create a class object to reverse the given list.
Example: Input = [1,2] and the expected output will be [2,1]
This is my code:
class ListNode(object):
def __init__(self, nums):
self.nums = nums
def tryout (self):
self.nums.reverse()
print(self.nums)
p = ListNode([1,2])
p.tryout
However when I ran my code on Leetcode, it kept giving me the error below
NameError: global name 'Solution' is not defined
ret = Solution().reverseList(param_1)
Line 33 in _driver (Solution.py)
_driver()
Line 43 in <module> (Solution.py)
Can I check if there's any issue with my code? i tried to run on Jupyter and the codes work but it didnt work when i submit to Leetcode

Sounds like Leetcode is expecting your class name to be Solution and your method name to be reverseList

Related

Why does class object have no attribute [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
Trying to build a Trie branch. I have looked at my indentation and there are no spaces, just tabs for that. I have also looked at other answers which either aren't reproducible or have no effect. What am I doing wrong? I expect the following outcome:
*
trie.py
class Trie:
def __int__(self):
self.root = '*'
main.py
import trie_test
if __name__ == '__main__':
trie = trie_test.Trie()
print(f'trie root: {trie.root}')
Error
line 13, in <module>
print(f'trie root: {trie.root}')
AttributeError: 'Trie' object has no attribute 'root'
def __int__ should be def __init__.

printing data member using class name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
class Acc:
id=121
def _init_(self,id):
self.id=id;
acc=Acc(111)
print(Acc.id)
When I try to run this code , the code runs fine and gives 121 as output when I directly call the data member 'id' using class name Acc and remove the object creation line from the code but the code gives error "TypeError: Acc() takes no arguments" when I include the second last line i.e. the object creation line in my code.
You need to use double underscores when creating constructor method:
...
def __init__(self, id):
...
In your question code you just created a method _init_.

Python how should I fix this if I call my function, it returns function object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I tried to call my function here and it returns function get_song_input at 0x7ff9869ee050
whats wrong with my code? I put it in python visualizer it worked out fine.
Album = namedtuple('Album', 'id artist title year songs')
Song = namedtuple('Song', 'track title length play_count')
def get_song_input():
"""Prompt user input."""
songtrack = input('Enter the song\'s track:\n')
songtitle = input('Enter the song\'s title:\n')
songlength = input('Enter the song\'s length:\n')
songplaycount = input('Enter the song\'s play_count:\n')
song_info = Song(songtrack, songtitle, songlength, songplaycount)
return song_info
print(get_song_input)
output:
<function get_song_input at 0x7ff9869ee050>
The function definition does not execute the function body; this gets executed only when the function is called.
To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")
my_function()
As others have stated, you need the brackets, i.e.:
print(get_song_input())

AttributeError: 'NoneType' object has no attribute 'n' in list of class objects [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Oh that's true. And now that code works but my long code still doesn't. Here it is https://pastebin.com/Z4qjGxCU. The problem with this one is that, when I call check_num() on the tiles, it works. But when I call check_mouse(), load_image() [which I've commented so you don't have to download the assets] or print_tile() they return an error such as this:
Traceback (most recent call last):
File "C:\Users\sergio\Desktop\Minesweeper\minesweeper.py", line 96, in <module>
grid[i][j].check_mouse()
AttributeError: 'Tile' object has no attribute 'check_mouse'
I've checked the code but I can't find anything that's wrong. Any ideas?
Define a class with class A: instead of def A()

Syntax error using def on python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My qestion is that my def kept on getting a invalid syntax.
l=0
a=1
b=1
i=int(input("How many iterations?" )
def mains():
while i>l;
l=l+1
a=a+b
print(a)
def mains()
Here is the error.
File "Fibonacci.py", line 6
def mains():
^
SyntaxError: invalid syntax
I am currently using 3.4.1 version of python.
Well firstly you have incomplete ) on the previous line:
i=int(input("How many iterations?" ))
^
and secondly, it should be : not ;:
while i>l:
and thirdly, to call a method, you just do:
mains()
not def mains()

Categories

Resources