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 last year.
Improve this question
adding = int(5+3)
subtract = int(10-2)
multiplication = int(2*4)
division = int(16/2)
print (str(adding,"\n",subtract,"\n",multiplication,"\n",division))
im getting a typeError: TypeError: str() takes at most 3 arguments (7 given)
Solution:
Just change this piece of code from this
print (str(adding,"\n",subtract,"\n",multiplication,"\n",division))
To this.
print (adding,"\n",subtract,"\n",multiplication,"\n",division)
Reason: str() is a typecast method. You don't need to typecast each and everything.
For Reference:
https://www.geeksforgeeks.org/type-casting-in-python-implicit-and-explicit-with-examples/
Think of str as a function that takes in a variable and converts it into a string. Passing multiple variables to str() separated by comma will not apply string function to each variable. It will instead mean that they are arguments to the function.
If you want to convert each variable into
adding = str(int(5+3))
subtract = str(int(10-2))
multiplication = str(int(2*4))
division = str(int(16/2))
print (adding,"\n",subtract,"\n",multiplication,"\n",division)
Note that print function accepts multiple arguments for printing purposes.
To convert an integer to string, you need to use str() each variables separately.
Also, you can use + as concat operator to avoid extra space.
print(str(adding)+"\n"+str(subtract)+"\n"+str(multiplication)+"\n"+str(division))
Related
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 1 year ago.
Improve this question
I am using f format to display results in the following code:
numb1 = input("Enter the first number here: ")
numb2 = input("Enter second number here: ")
print(f"Product of {numb1} and {numb2} is {numb1 * numb2}"
SyntaxError: unexpected EOF while parsing
Anyone has any idea to where I am getting it wrong? I researched and could not find the answer.
Thank you
numb1 and numb2 are both str-typed (because that's what input returns): you need to coerce them to int for the * operation to be valid inside the f-string. Strings do have a __mul__ method, but (a) it's not defined for an operation against another str (which is what you're attempting to do here), and (b) the product of an str and an int won't give you the result you want (rather, 'a' * 5 produces the repeated character sequence, also an str).
Assuming the other issues in your code are a result of your formatting (e.g. missing newline or ; between input statements and no closing ) in print), that's exactly what the exception will tell you (a TypeError).
One correct approach:
numb1 = input("Enter the first number here: ")
numb2 = input("Enter second number here: ")
print(f"Product of {numb1} and {numb2} is {int(numb1) * int(numb2)}")
int truncates, so if digits to the right of the decimal point should be correctly handled in your input, you'll want to perform the coercion with float (assuming floating-point error is acceptable) instead.
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 want to get the sum of the digits of an integer, and found the solutions below:
sum(map(int,list(str(123))))
sum(int(digit) for digit in str(123))
However, when I run this, I get the below error:
TypeError: 'int' object is not callable
Contrary to other answers, both your code works fine.
I think you are shadowing something on your code. Perhaps you used int as a variable?
sum() works on iterables.
int(digit) for digit in str(123)
This returns an generator, and should work, as said by other answers, take a look at this answer.
The below should also do the job:
sum([int(digit) for digit in '123'])
Hope this helps!
sum(int(digit) for digit in str(123))
The above code should work.
However you said you get the error,
TypeError: 'int' object is not callable
That error suggests that you're using type int instead of a str
.
Did you use another variable for that?
For example, the below code should give you that error you mentioned
obj = 123
sum(int(digit) for digit in obj)
You have to ensure that obj is of a str type.
sum() works in iterable objects. You need to create a list with the digits you are trying to add. The code below should do that:
sum([int(x) for x in str(123)])
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
I'm attempting to bulk-download some csv files from a website. I've included a generic form of the first few lines of code I'm using below.
import urllib3
import os.path
def downLoadToDir(save_path,foo):
http = urllib3.PoolManager()
os.makedirs("".join(save_path, foo)
# Set up url and path for download
VarUrl = "".join("http://url.com/ajax/exportKR2CSV.html?t=", foo)
VarPath = "".join(save_path, foo, '/',foo, '.csv')
Ideally this should set up a folder under the specified filepath, and set up two variables I use later. However, I keep getting this error:
File "url_download.py", line 10
VarUrl = "".join("url.com/ajax/exportKR2CSV.html?t=", foo)
^
SyntaxError: invalid syntax
Based off of other examples I've seen online, this seems correct to me. Nothing seems to make it happy. Where am I going wrong? Thanks
You're missing a right parenthesis at line:
os.makedirs("".join(save_path, foo)
Also, the join method takes only one list argument, and you are passing two arguments here. You should make the two strings a list before passing to join as one argument:
os.makedirs("".join([save_path, foo]))
The same issue goes for the following lines that also use join.
You are not closing the bracket on line 7. Python is still looking for the close bracket and the "" is not what the compiler expects to see.
use os.makedirs("".join(save_path, foo))
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 5 years ago.
Improve this question
I have used str as a variable. Now, I would like to convert an int into a string. For this, normally I would use str(10). What should I do in this case?
You can just delete the variable to get str() back:
othervarname = str
del str
Find and replace "str" with "sensibleNameForYourVariable", then use str(i) to convert integers to strings.
Use the code below if you had used str as a variable. This will solve your problem. The keyword str() internally uses __str__() function so better use that function only
str = 1
str.__str__()
Output
'1'
As in your case you should use.
Retrieve it from __builtins__.
str = "shadowed"
assert __builtins__.str(3) == "3"
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 9 years ago.
Improve this question
For some reason this piece of script is returning the error: "TypeError: string indices must be integers"; and I cannot see what's wrong. Am I being stupid and overlooking an obvious mistake here? I can't see one for the life of me!
terms = {"ALU":"Arithmetic Logic Unit"}
term = input("Type in a term you wish to see: ")
if term in terms:
definition = term[terms]
sentence = term + " - " + definition
print(sentence)
else:
print("Term doesn't exist.")
I think you want it this way: definition = terms[term]
This line definition = term[terms] is trying to get a character out of the string term. You probably just typoed, and want
definition = terms[term]
^ here, reference the dict, not the string
You are indexing the string term instead of the dictionary terms. Try:
definition = terms[term]
You accidentally swapped the variables. Change this:
definition = term[terms]
To this:
definition = terms[term]