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
I am new to python and I have this script to set a variable:
set python = 1
print (python)
I have tried this
set (python) = 1
print (python)
But this doesn't seem to work ether. Can any one help.
This is not Python. In Python, to set a variable, use:
name = 1
And reassign:
name = [1, 2, 3]
EDIT:
The exercise asks you to set the variable my_variable to 10.
So use:
my_variable = 10 # set my_variable to 10, and use your brain
No need to write set. Its not a python syntax.
Python is dynamically typed and creates objects as it goes along. You don't really have constants or anything along those lines (at least at a beginners level - I'm not sure about advanced levels but I doubt it).
As a result whenever you declare a variable just input:
RandomVariableName = 1
or
RandomVariableName = "one"
Python will automatically figure out what the type of object is and assign it to that name (loosely speaking).
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 have a list of strings:
mini_corpus = ['I am Sam','Sam I am','I am Sam','I do not like green eggs and Sam']
I need to add a sentence boundary at the beginning and end of each element (i.e. 'BOS I am Sam EOS', 'BOS Sam I am EOS', etc.)
I've tried using map : mini_corpv2 = list(map(lambda x: 'BOS{}EOS'.format(x), mini_corpus)) but it throws 'list' object is not callable
Can anyone tell me what I'm doing wrong or suggest another method to implement this?
I suppose the problem is somewhere else. Your code runs without problems, resulting in
['BOSI am SamEOS',
'BOSSam I amEOS',
'BOSI am SamEOS',
'BOSI do not like green eggs and SamEOS']
(so you will probably want to add spaces after BOS and before EOS).
An alternative solution using list comprehension:
mini_corpv2 = [f'BOS {x} EOS' for x in mini_corpus]
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'm learning Python and there is a problem where I got stuck. I would definitely appreciate it if anyone can help to solve my doubts.
When I typed this code:
i=1 and i<=10
print(i)
the output is True. Obviously i is a boolean now but I don't understand why.
For there is an "and", and 1 is less than 10, so the statement "i=1 and i<=10" is true. But why the variable i (rather than the whole statement) becomes a boolean? I thought i should still be an integer whose value is 1?
It's a beginner's question but it really confuses me. Thanks for anyone who contributes an idea!
By your code , I think that you are setting ‘1 and i<=10’ into your i variable.
If you want check equal to 1 , use ‘==‘ and add an if sentence, so your code will be:
if i==1 and i<=10:
print(i)
The statement is to the right of the assignment operator =
1 and i<=10
i is not defined at this point in your example, so a NameError would be raised. I assume that you defined i somewhere before running this code and didn't see the error.
Had i been defined, the result of calculating this statement would be assigned back to i as it is to the left of the assignment operator =. Its the same as
i = (1 and i <= 10)
If you want to assign i before the comparison, then it needs to be in another statement
i = 1
i <= 10
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 am very new to Python and trying to learn by trial-and-error, so my question may sound naive for the community.
Let's say I have two empty lists with only the first element defined:
a = [[]]*20
a[0] = 0
b = [[]]*20
b[0] = 1
I want to use a for loop for creating the other elements of the lists:
x = 20
for i in range(1,x):
a[i] = b[i-1],
b[i] = a[i-1]+b[i-1]
What I obtain is the following error:TypeError: can only concatenate tuple (not "int") to tuple.
Basically I am trying to reproduce the fibonacci series (a famous starting point in Python tutorial), but I would like to experiment other ways of obtaining the same output.
Thank you!
The problem is on this line:
a[i] = b[i-1],
Notice the comma at the end? That makes python think you're dealing in tuples. Remove it and the error will be gone.
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 years ago.
Improve this question
For some odd reason, my code won't work in Visual Studio on my laptop. It gives me errors on my script. Am I doing it wrong?
The errors I got were:
Can't assign to errorexpression --line 2
Unexpected indent --line 2
Unexpected token '<dedent>' --line 6
print("welcome user")
varpassword = input("Please enter a password: ")
if varpassword = "thisisthepassword123":
print("Welcome")
else:
print("access denied")
As others have pointed out your conditional statement should use the == operator (to indicate that you are comparing the two values to see if they're equal) instead of = that assigns the value to the variable.
if varpassword = "thisisthepassword123":
I just want to add that you should avoid using a hard-coded password value especially in python since it's plain text (unless this is just sample code to illustrate)
Edit:
Use a hashing algorithm to hash your password instead and then hash the user input and compare that. So you'll put the password through something like SHA1 or so (if you want to use a hard-coded value like "thisisthepassword123" it will have a value of f61c1bbcf1f7d68106a18bd753d4fc3c4925793f. So using a library like hashlib(https://docs.python.org/2/library/hashlib.html) you can do this:
import hashlib
hashlib.sha1(userinput).hexdigest()
Also consider using salting, read this: https://crackstation.net/hashing-security.htm
Edit 2:
Also make sure that your indentation in your script matches the indentation of your code snippet
please add == to compare = is use to assign
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
Hi I would like to store strings on a array. This strings are produced in this loop:
while (count < ts ):
dt=tb
t1=count+180
t2=t1+360
dt1=dt+t1
dt2=dt+t2
slice=stream.slice(dt1, dt2)
B=str(dt1)
E=str(dt2)
slice.write(station+'_'+comp[i]+'_'+B+'_'+E, format="MSEED")
count = count + 360
bb=[]
name=station+B+'_'+E
a=[str(name)]
bb.append(a)
But it doesn't work. The variable name is from type:
name=2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z
And I would like to have an array like that:
bb=[2011-05-22T23:42:00.000000Z_2011-05-22T23:48:00.000000Z, 2011-05-22T23:48:00.000000Z_2011-05-22T23:54:00.000000Z, 2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
But what bb returns me is an array with the last element called:
bb=[2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
If I do it manually:
bb.append('2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z')
It works perfectly because I put the ''. But I need to it in a automatic way.
Any suggestion?
Thanks in advance!
Declare bb outside the loop and a will be a list. You will get a list of lists(not in the way you asked for)