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
V=str(input ('\n\tEnter\n\n\t'))
if V.isnumeric:
print (f'\n\tYour V is a numeric and it is "{V}"\n\t')
When I enter an alphabet, it considers it as a numeric. How to make it only accept numbers or alphabets?
isnumeric is a function but you are not calling it so it is returning true as it is a defined function. Your if statement is checking whether isnumeric is defined for the variable V
You should do this -
V=str(input ('\n\tEnter\n\n\t'))
if V.isnumeric():
print (f'\n\tYour V is a numeric and it is "{V}"\n\t')
I think it should be V.isnumeric() there is some limitations on negative
Definition and Usage
The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.
Exponents, like ² and ¾ are also considered to be numeric values.
"-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the . are not.
This should be useful if you want to try a couple of functions: https://lerner.co.il/2019/02/17/pythons-str-isdigit-vs-str-isnumeric/
The function isnumeric() is not being called in your code presently, you are simply referencing it:
if v.isnumeric: # This resolves to a pointer to the isnumeric function
....
To call a function, you require v.isnumeric(). Note that in the original (without parentheses) it resolves to function object, which in turn evaluates to True (essentially, because it exists at all) when in an if clause. However, the function itself is never run. It's for this reason that you're seeing it print out every time.
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 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 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
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 8 years ago.
Improve this question
I'm trying to keep a string I've converted from an integer. I can convert an int variable into a string, but then I can't seem to interact with that. Here's how it goes in the Python shell:
a = 100
a
>>> 100
str(a)
>>> '100'
a
>>> 100
str(a) = b
>>> SyntaxError: can't assign to function call
What I need is to turn this '100' string into a new variable. I've tried searching; the answer is probably out there, but I'm clearly not using the right search terms. All the answers I've found have only been concerned with how to convert from one type to another.
The problem you are experiencing is not the problem that you think you are experiencing.
When you define a variable in Python, the variable goes on the left of the equals sign. It should look like this:
b = str(a)
This will define b without giving you an error message. Going back to your question, if you want to change a to a string and keep it that way, str(a) will not suffice. Instead:
a = str(a)
Will change your variable to a string. str(a) is simply a function that returns the variable a in the form of a string. If you do not redefine a here, str(a) will not return to anywhere and your string will be lost.
You have your assignment syntax back to front. The target name goes on the left:
b = str(a)
Now b is a reference to the return value of str().
You can also re-assign back to a, replacing the old integer value with the string representation:
a = str(a)
Your attempt instead tried to use str(a) as an assignment target; Python can't let you do that because the result of str(a) is unknown when the code is compiled; you cannot bind any object to another, you need to have a reference instead (so a name, or an attribute, or a list index, or a dictionary key, etc.).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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.
Improve this question
I have a data set as follows it includes some negative values. I have written codes to read those values. But when any negative numbers percent. It gives error. File format is as below
1SOL HW1 2 1.4788 2.7853 -0.7702
1SOL HW2 3 1.4640 -2.8230 0.6243
2SOL OW 4 -1.5210 0.9510 -2.2050
2SOL HW1 5 1.5960 -0.9780 2.1520
I have written code as follow. I am using for loop and if function to select P[3], P[4], P[5] positions.
X=[]
P = line.split()
x = float(P[3]) `#then I collect these numbers in to array like X = []`
X.append(x)
This code work if there is no negative values.
Then I used following function to write X in to another file. But it is not working
A.write('%s\n' % (X)) `# this is not working since it X is Float. File open as A to write`
Please some one help me to correct my cords.
The reason A.write('%s\n' % (X)) doesn't work has nothing to do with X being a float.
There may be a problem in that (X) isn't a tuple of one float as you seem to expect, it's just a float. Commas make a tuple, not parentheses. In particular, comma-separated values anywhere that they don't have some other meaning (function arguments, list members, etc.) are a tuple. The parentheses are only there to disambiguate a tuple when the comma-separated values would otherwise have another meaning. This is usually simple and intuitive, but it means that in the case of a one-element tuple, you need to write (X,).
However, even that shouldn't be a problem: '%s\n' % 3.2 is '3.2\n'.
On top of that, X isn't actually a float in the first place, it's a list. You explicitly created it as X = [], and then appended each float to it. Again, that's not a problem, but it means you're possibly not getting the output you wanted. This is just a guess, since you never explained what output you wanted or what you were actually getting. But '%s\n' % [3.2, 3.4] is '[3.2, 3.4]\n'. If you wanted each one on a separate line, you have to loop over them, explicitly or implicitly—maybe ''.join('%s\n' % x for x in X).
As for why your negative numbers don't work, there are many possibilities, and it's impossible to guess which without more information, but here are some examples:
There is something that Python (more specifically, split()) considers whitespace, even if it doesn't look like it to you, between the - and the numbers. So, you're trying to convert "-" to a float rather than "-12345".
Those apparent - characters are actually a Unicode minus rather than a hyphen, or something else that looks similar. .decode-ing the file with the right encoding might solve it, but it might not be enough.
There are invisible, non-spacing characters between the - and the first digit. Maybe Unicode again, or maybe just a control character.
In many cases, the exception string (which you didn't show us) will show the repr of the string, which may reveal this information. If not, you can print repr(P[3]) explicitly. If that still doesn't help, try print binascii.hexlify(P[3]) (you'll have to import binascii first, of course).
It's impossible to tell what will work because we can't see your source file, but the problem you're having is with your split function. If I were you, I'd try P = line.split('\t') and see if that resolves your issue.