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.
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'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.
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 know the title is a bit confusing, but what it means is this:
abdc123 is my reference string and I want to know if this string appears on all other strings. Here's the code:
if ref_string in target_string:
print("True")
if target_string says something like "abcd123 asdf789 bnc1222", it will return as True.
However if target_string says something like "asdf789 bnc1222 abcd123", it will now say False even if abcd123 is clearly there. It only works if abcd123 is the first string? How can I fix this? Thanks a lot!
PS: target_string is a string, not a list. It's like a sentence.
If I take the reference and target strings which you specified, I get a True as expected:
ref_string = "abcd123"
target_string = "asdf789 bnc1222 abcd123"
print(ref_string in target_string)
This outputs True.
Note that in the opening line to your question, you've referenced a subtly different string (abdc123), so do make sure to check that your reference string really is in the target.
Split the string target_string which casts it to a list and then compare ref_string with that list as follows:
ref_string='abdc123'
target_string="abdc123 asdf789 bnc1222"
a=target_string.split()
if ref_string in a:
print("True")
else:
print('False')
Edit: As mentioned in the comment in works on the string as well, and yes that is another method and it works fine too.
ref_string='abdc123'
target_string="asdf789 dfbfb abdc123"
if ref_string in target_string:
print("True")
else:
print('False')
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 having trouble getting Python's .upper and .lower methods to return anything. Here's the code:
initials = input("Enter your initials: ")
uppercase = initials.upper
print(uppercase)
What it returns is:
Enter your initials: mj
<built-in method upper of str object at 0x7f50734b01f0>
I originally integrated this into a larger function, but when I call uppercase later in the function the variable remains empty. I'm working in Google Colab.
To call a method in Python, you must open and close parenthesis after the name of the method:
uppercase = initials.upper()
See the example in the documentations of str.upper.
Here you go, just make sure you use .upper() method appropriately with your variable. :)
initials = input("Enter your initials: ")
print(initials.upper())
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 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