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'm very new to coding and I am learning python alone and this is my code :
from random import *
temp =Randint( 0,70 )
print(temp)
if : temp = 69
print("nice")
You made a few errors
randint is a function so it's first letter should be small case
The for loop syntax was wrong
And we use == for checking equality and = for assignment
You can try the following code below
from random import randint
temp = randint( 0,70 )
print(temp)
if temp == 69:
print("nice")
Your if statement isn't quite right. The colon needs to go at the end of the line, and in an if statement you need a double equals sign (==, which compares two elements) rather than a single equals sign (which assigns a variable). Additionally, you need to indent (put a tab before) lines that are part of a block. You can read more about Python if statements here. The fixed if statement should look like this:
if temp == 69:
print("nice")
Lastly, since Python names are case-sensitive, randint must be in all lowercase.
I hope this is helpful!
A quick thing to remember is that “==“ means two things are equal and “!=“ means if something is not equal.
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 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 3 years ago.
Improve this question
I want to solve this error.
i tried for almost half hours, but I couldn't find the answer..
This is my error
File "sampling_fun.py", line 71
def average(self) :
^
SyntaxError: invalid syntax
and full code
import csv
class fun :
def __init__(self, rowList, num) :
list = []
listLen = len(self.rowlist)-1
for i in range(listLen) :
list.append(self.rowList[i+1][num] # num = Header 1~4
def average(self) :
ave = sum(self.list)/self.lestLen
print("average : %0.2f" %ave)
return ave
testlist = cssRead('Data_2', 1)
test1 = fun(testlist, 1)
test1.average()
When you encounter such error, check the line before it.
You have a non-matching paranthesis, the closing brace for append is missing.
list.append(self.rowList[i+1][num])
def errors happen when python is not expecting a function definition. As #Siong Thye Goh notes, a missing ) is your issue. For the future, the only time python does not expect a function definition is after an incomplete code-block.
That can happen when you forget to close parentheses or brackets, but also when you forget to put a statement after a colon or don't indent correctly.
Rarely, it can happen due to incompatible space characters when copy-pasting from external sources.
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 6 years ago.
Improve this question
I'm still getting a handle on python and have been trying to implement a REPEAT UNTIL loop using online tutorials. Everything seems to be in order but I keep getting a syntax error but I absolutely cannot find an error with my syntax! Can someone help me? My code is;
while detvar != "SABRE":
REPEAT
detvar=input("Please Pass a Valid Detector or Parameter Set");
UNTIL detvar = "SABRE"
detvar is my string variable the error is for the 'detvar' on the last line.
This is all you need:
detvar = "" # allow for at least one iteration
while detvar != "SABRE":
detvar=input("Please Pass a Valid Detector or Parameter Set")
REPEAT and UNTIL are not valid expressions in Python. Instead, you want to use while condition != value, which is what you originally had.
The while statement allows you to continue iterating as long as a condition holds true. Alternatively, you can repeat until something is true by negating the condition.
So, while detvar != "SABRE": iterates the body of the loop (which is everything indented under the colon) until detvar is equal to "SABRE".
Edit: In accordance with Bryan Oakley's comment, detvar is initialized as a value that is not "SABRE" so that the loop body executes at least once.
This is a horribly worded question, and I have no idea what this code is meant to accomplish, but I'll see if I can decipher this. There's no need to use "REPEAT," just do
while devtar != "SABRE":
devtar = input("Please Pass.(whatever this is).. Set")
It should exit the loop when devtar = "SABRE" automatically.
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 7 years ago.
Improve this question
Hi I've been trying to write a program in python 2.7 that takes a word as its input and outputs the number of letters in a word. At first it was working but something happened and now it keeps returning an error by the first line that is not part of the while loop.
This is a part of the code:
def number_of_letters(input):
nol = input.find(input[-1])
while input[nol:] != input[-1]:
nol = input.find(input[-1], input.find(input[-1] + 1)
nol = nol + 1
print nol
The python interpreter keeps returning a syntax error by whatever I try to put after the while block (in this case 'nol = nol + 1 ')
I've tried playing around with it but nothing worked. PLease help.
By the way if there are any modules that may help with this program that would be great but I'd also like to know why this one isn't working
You are missing a closing paren:
nol = input.find(input[-1], input.find(input[-1] + 1)) #<- add here
If you want to count the number of actual letters you can use str.isalpha:
return sum(ch.isalpha() for ch in inp)
If you don't care what characters are there just use len(inp).
Avoid input as a variable name as it shadows the python function.
Change this
nol = input.find(input[-1], input.find(input[-1] + 1)
to this
nol = input.find(input[-1], input.find(input[-1] + 1))
Notice that parenthesis in the end.
There is a built in function for getting the length of a string in python.
word = "test"
length = len(word)