This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
How can I concatenate str and int objects?
(1 answer)
Closed 6 months ago.
So, I want this code to have user input Dogechain address, and I get the balance of that address. When I execute this I get error:
url = "https://dogechain.info/api/v1/address/balance/"+ a
Error:
TypeError: cannot concatenate 'str' and 'int' objects
Code:
import requests
def main():
a = input("Address?")
url = "https://dogechain.info/api/v1/address/balance/"+ a
response = requests.get(url)
bal = response.json()[0]["balance"]
print("Balance:",bal)
main()
url = "https://dogechain.info/api/v1/address/balance/" + str(a)
or
url = "https://dogechain.info/api/v1/address/balance/{}".format(a)
And #Ryan O'Donnell's comment below explains why, thanks.
Related
This question already has answers here:
Using a dictionary to select function to execute
(11 answers)
Closed 6 months ago.
How can I call a (string) method assigned to a dict value like this
dict = { True: ''.lower }
I tried
flip = True
print("A".dict[flip])
but then I get:
AttributeError: 'str' object has no attribute 'dict'
d = {True: lambda x: x.lower()}
flip = True
print(d[flip]('A'))
prints
a
This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 1 year ago.
How to pass a variable for an f-string '=' debugging operator?
from datetime import datetime
def print_var(var):
print(str(datetime.now())[:19], end=' ')
print(f'{var = }')
test = 5
print_var(test)
I expect print_var(test) to print the variable name from outside the function, ie.
test = 5
Please refer to Python: Print a variable's name and value? for the context of the question.
In f-strings you need to put the variable that needs to be printed between accolades. Like this:
from datetime import datetime
def print_var(var):
print(str(datetime.now())[:19], end=' ')
print(f'var = {var}')
Running the test yields the following:
test = 5
print_var(test)
>>>2021-10-06 11:32:05 var = 5
This question already has answers here:
TypeError: 'list' object is not callable while trying to access a list
(9 answers)
Closed 6 years ago.
I always get the Error: 'List' object not callable... I looked around in Google and tried every given solution, but it's still the same.
I cannot get my code to work. I have a list of integers, and I need to give every element to different variables.
dmy = input('What is your date? Please put in like this: 2.11.2016')
dmy.strip(".")
dmy = [int(x) for x in dmy.split('.')]
list(dmy)
print(dmy)
dd = dmy(0)
mm = dmy(1)
yy = dmy(2)
The first part of the code is working. I get the error while trying to give the list element to another variable so this dmy(0) does not work. But it is in all the books I have this way?
I use python 3.5.2
I see what you are trying to do. An element in the list is obtained by list[index] format. While you are trying to call as list(index) which python is interpreting as function call and hence throwing you error:
TypeError: 'list' object is not callable
Corrected code:
dmy = input('What is your date? Please put in like this: 2.11.2016')
dmy.strip(".")
dmy = [int(x) for x in dmy.split('.')]
list(dmy)
print(dmy)
dd = dmy[0]
mm = dmy[1]
yy = dmy[2]
>>> dd = dmy[0]
>>> mm = dmy[1]
>>> yy = dmy[2]
>>> dd
2
>>> mm
11
>>> yy
2016
>>>
This question already has answers here:
What is the purpose and use of **kwargs? [duplicate]
(13 answers)
Closed 7 years ago.
I'm testing to use kwargs and have a trouble.
This is the python3 code :
class B:
def __init__(self, **kwargs):
print(kwargs['city'])
a = {'phone':'0101', 'city':'Daejeon'}
b = B(a)
But, there is an error like below :
b = B(a)
TypeError: __init__() takes 1 positional argument but 2 were given
Is there something wrong in my code?
I think that I just exactly follow the tutorial....
Keyword arguments are not passed that way.
obj1 = B(phone='0101', city='Daejeon')
obj2 = B(**a)
This question already has answers here:
How can I represent an 'Enum' in Python?
(43 answers)
Closed 8 years ago.
I have declared the enum as follows in python.I don't know how to use them.When I create an instance of this class it gives error as two arguments are required one given.
class CBarReference(Enum):
ThisBar = 0,
NextBar = 1,
Undefined=2
a=CBarReference()
I know what error is but I don't know what to give as the second argument other than self.
You should never have to create an instance of an enum; they're all accessed directly from the class, and you can just assign them to variables as you like:
a = CBarReference.ThisBar
b = CBarReference.NextBar
c = CBarReference.Undefined
d = CBarReference.ThisBar
assert(a == d)
assert(b != a)
assert(b != c)