Why is this simple 3 line program not working? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
class Pract:
p1 = Pract() #creating an instance of Pract
p1.age=45 #creating a field-variable
print(p1.age)
I checked this Youtube, in the video its shows as working, but I couldn't run it.

# First declare a class (empty, in this case)
class Pract:
pass
# Then instantiate it
p1 = Pract()
# Then set the attribute
p1.age = 45
# Then print the attribute
print(p1.age)
You cannot instantiate a class before you finish declaring it. Everything you put inside class is part of the class definition. You have to de-indent your code in order to mark the end of the class definition.

Related

Im getting attributeError: 'str' object has no attribute 'zipcode' when running this code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My code:
import requests
class weatherapi:
def __init__(self,api_url,string,zipcode):
self.api_url = 'https://api.openweathermap.org/data/2.5/weather?zip=10502,us&appid=50b55ef1602086c74b71f56c6df14996'
self.input = string
self.zipcode = zipcode
def get(self):
get_weather = requests.get(self.api_url)
json_weather = get_weather.json()
def response(self):
national = requests.get('https://api.openweathermap.org/data/2.5/weather?zip='+self.zipcode+',us&appid=50b55ef1602086c74b71f56c6df14996')
json_national = national.json()
kelvin = float(national.json['main']['temp'])
return str(kelvin)
from weatherapi import weatherapi
def main():
zipcode = input('Please input your zip code ')
weatherapi.response(zipcode)
I get attributeError: 'str' object has no attribute 'zipcode' when running this code. I have no idea why this is happening, could somebody explain why?
The function .response does not take any arguments aside from the reference to the object ("self").
You need to first construct an instance of the class before you can use the .response() function. You'll need something like `my_weatherapi = weatherapi('https://www.some.url.com/', 'some_input_string', 90210).
Also, the error code is AttributeError, not tributeError.
All in all, there are a lot of mistakes in this. I would recommend you go through a Python tutorial first; specifically look into defining classes and using constructors and class functions.

module as no attribute - self.variable.function(self) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a module call pcb_files.py that only have some imports like this -> import read
Then I have another module call Easy.py, that have a class (Mainwindow) and a method/funtion (function_pcb1)
Class MainWindow(xxx,xxx)
.....
.....
def func_pcb1(self):
pcb_files.read.main(self)
Right now everytime I press a pushbutton in my app I run the funtion "main" that is inside "read". So far so good
What I want:
def func_pcb1(self):
script=self.nome_do_script
pcb_files.script.main(self)
Like you see in, now I have this : script=self.nome_do_script where "script" is a string type.
And now I just want to change one thing, in the place of "read" I want to put the "script" like i do in the image but it gives me an error -> AttributeError: module 'pcb_files' has no attribute 'script'
Resuming, instead of call whats inside "script" variable, it's calling the name script itself.
Now you're asking why do you want that ? -> Answer: I want to call, and show to the user, in my app different files that will do different things
something.other is what Python calls attribute access, where "other" is the attribute name. If you want to access an attribute with a dynamic name, you can use getattr.
def func_pcb1(self):
script = self.nome_do_script
getattr(pcb_files, script).main(self)
In the long term, you'll want to learn how to use dictionaries for these kinds of things.

When using self in a function doesn't let me invoke the function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Sorry if it's a stupid question but I'm a beginner and couldn't find the answer on google, so I thought on asking here to learn. When I write
Class Buttons:
def play_b(self):
self.play_button = Button(main_window, text="Play")
self.play_button.grid(row=0, column=0)
It returns TypeError: play_b() missing 1 required positional argument: 'self'
However, when I write the following it works perfectly:
class Buttons:
def play_b():
play_button = Button(main_window, text="Play")
play_button.grid(row=0, column=0)
My question is: Why this happen? shouldn't functions always have the self keyword?
Edit: This is all the code so far: this is all the code so far:
from tkinter import *
main_window = Tk()
main_window.geometry("720x480")
class Buttons:
def play_b():
play_button = Button(main_window, text="Play")
play_button.grid(row=0, column=0)
Buttons.play_b()
I believe you've messed up the method call as mentioned in the comments. Call the method like so
obj = Buttons() # assuming no parameterized __init__ (constructor) method
obj.play_b()
In the second case, it's a static method i.e. it doesn't do anything with the object's (self) data, and so it works. But the changes won't be reflected in the object. See more on static methods here.
EDIT
After you updated the question, I see that you've called the class method directly, without instantiating it. Hence the problem. The solution is still the same as what I've mentioned before. Create an object (instantiate) first, then call the method using that object.

Counting and Grouping function with Python (2.7) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have tried the following function:
def item_order(order):
salads = order.count("salad")
hamburgers = order.count("hamburger")
waters = order.count("water")
return "salad:{} hamburger:{} water:{}".format(salads, hamburgers, waters)
taken from ( https://stackoverflow.com/questions/34906570/counting-and-grouping-with-python ),
with these two orders:
1st order = "salad water hamburger salad hamburger"
- then the function should returns "salad:2 hamburger:2 water:1"
2nd order = "hamburger water hamburger"
then the function should returns "salad:0 hamburger:2 water:1",
in http://www.pythontutor.com/visualize.html#mode=edit
But it seems it doesn't work.
Maintaining this structure, what am I doing wrong?
Many thanks for any help!!
You have a function definition, and then in your script you define the order:
def item_order(order):
# your function here
order = 'salad water hamburger salad hamburger'
When you call the function, you need to either assign the result to a variable or otherwise display the return from the function. So:
print item_order(order)
or:
x = item_order(order)
print x
Your code works as intended.
Variable order needs to be assigned some value and then function item_order can be called with variable order as the argument.

Create a new type in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there a way in Python 3.x to create a new type? I can only find ways to do it in c++. (I don't mean adding/editing syntax by the way)
Basically what I want to do is create a lexer, where I scan input and python can already do int and string, but if I want another datatype such as name, how could I assign it so that I can do...
Example:
# This can be done
a = "string"
type(a)
> <class, 'str'>
# How can I do this?
b = myName
type(myName)
> <class, 'name'>
You would want something like this, a class. In the source code all of the object types you see in Python are in class form.
>>> class myName:
... def __init__(self, name):
... self.name = name
... def __str__(self):
... return self.name
...
>>> b = myName('John')
>>> type(b)
<class '__main__.myName'>
>>> print(b)
John
The reason the output is slightly different to what you expected is because the name of the class is myName so that is what is returned by type(). Also we get the __main__. before the class name because it is local to the current module.
You might have a look at Metaclasses: http://eli.thegreenplace.net/2011/08/14/python-metaclasses-by-example/
However, what exactly do you want to achieve?
The short answer is you can't make a new type in python without editing the source code (written in C).
However the answer about creating a class in python is probably the easier route to go since editing the source can create compatibility problems with packages (potentially speaking).

Categories

Resources