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 write code which will output some data file, but I got error and have no idea what is wrong already looking for 2 hours (you know how is it :) )
import binascii
import sys
import time
url12 = "my win dir for file"
def toHex(s):
1st = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
1st.append(hv)
return reduce(lambda x,y:x+y, 1st)
url = toHex(url12)
.......etc.
debug output:
Traceback (most recent call last):
File "C:\Users\RED\Desktop\builder\builderUpdate.py", line 22, in <module>
url = toHex(url12)
File "C:\Users\RED\Desktop\builder\builderUpdate.py", line 20, in toHex
return reduce(lambda x,y:x+y, lst)
NameError: name 'reduce' is not defined
In Python 2, reduce was a built-in function. In Python 3, you have to import it from functools
That's why you're getting an error
Also, the empty list in your function can't begin with a number otherwise you get a syntax error. It can contain numbers but should start with an underscore or a letter
Related
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 am inputting simple code into IDLE but it keeps telling me after I run the simple program that a word to check how many vowels is not defined.
def search4vowels(word):
'''Return a boolean bassed on any vowels found.'''
vowels = set('aeiou')
return vowels.intersection(set(word))
Error Message
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
gal
NameError: name '' is not defined
I think you call your function like this:
search4vowels(gal).
Try calling it like this: search4vowels("gal").
You have to enclose strings in quotes, otherwise python is looking for a variable called gal (In your example).
gal is not defined==>Error
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 3 years ago.
Improve this question
So basically I have to write a code which tells the user to input numbers (as variables I named them number1, number2, number3 ...) until I get the answer of zero once you add each number that they inputted then which the sum is equal to zero I should print how many numbers they inputted so the answer they got was zero, now the main thing is that I know how to write that code but there is an error that I get:
Traceback (most recent call last):
File "Solution.py", line 13, in <module>
sys.exit()
NameError: name 'sys' is not defined
Maybe try using
from sys import *
and just use exit(0) when you want to sys.exit()
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 5 years ago.
Improve this question
i am very new to python.
i would like to write a script where if i didn't pass any value to an argument, it should ask value for that argument. if passed, it should pick that value and continue.passing these values are from command line.
i tried below code and python is throwing error saying variable is not initialized.
if (fileName == None)
fileName == "C:\\filename"
print(fileName)
command line call for executing the script:- script.py "C:\filename"
Stack trace :-
if(NO_ERROR == None)
^
SyntaxError: invalid syntax
You are missing a colon at the end of the if statement
if (fileName == None):
fileName = "C:\\filename"
print(fileName)
sys module provides lots of options to play with command line arguments.
below example might be helpful to you.
import sys
#storing the argument in string
st=" ".join(sys.argv)
#splitting the list to get the file name and storing it in list
var=st.split('=')
if len(sys.argv)<2:
print "Enter Argument\n"
else:
print 'Argument found'
print var[1]
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 6 years ago.
Improve this question
i have this problem with my Python code :
from math import *
m = pow(complex(1,2)*complex(3,0) + complex(1,0),complex(-1,0));
TypeError: can't convert complex to float
Does anyone know how to solve this problem ?
Thanks a lot !
The problem is that you are using from math import *. This shadows the built-in pow with a version that doesn't support complex numbers.
>>> pow(1+1j, 1)
(1+1j)
>>> import math
>>> math.pow(1+1j,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
from <x> import * is usually considered bad practice, and cases like this are why.
Instead, you should use import math, and reference all your math functions as, e.g. math.sqrt
Alternatively, you can use ** instead of pow:
>>> 1j ** 2
(-1+0j)
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 tried to solve the following problem by creating a new file, then tried each one of these functions to get the file extension but all i got was errors.
What am I doing wrong?
"Examine the following three functions that take as argument a file name and return the extension of that file. For instance, if the file name is 'myfile.tar.gz' the returned value of the function should be 'gz'. If the file name has no extention, i.e. when the file name is just 'myfile', the function should return an empty string."
def get_extension1(filename):
return(filename.split(".")[-1])
def get_extension2(filename):
import os.path
return(os.path.splitext(filename)[1])
def get_extension3(filename):
return filename[filename.rfind('.'):][1:]**
Which of the these functions are doing exactly what they are supposed to do according to the description above?
a) get_extension1 and get_extension3
b) get_extension3 only
c) get_extension2 and get_extension3
d) All of them
get_extension1(filename) will return the file name if filename does not contain .
get_extension3(filename) will raise an error because of ** at the end:
SyntaxError: invalid syntax
get_extension1 shoud be:
def get_extension1(filename):
output = filename.split(".")
return output[-1] if len(output)>1 else ''
Try it and find out:
get_extension1("myfile.ext")
Out[60]: 'ext'
get_extension1("myfile")
Out[61]: 'myfile' # wrong
get_extension2("myfile.ext")
Out[62]: '.ext'
get_extension2("myfile")
Out[63]: ''
get_extension3("myfile.ext")
Out[64]: 'ext'
get_extension3("myfile")
Out[65]: ''
Edit: it sounds like the AttributeErrors are because you are passing something other than a string for filename. If filename is a string they run just fine, but get_extension1 fails if the filename has no extension.