Check a float value contains a minus sign (hyphen-minus) - Python
I just want to check the variable contains a negative value but it keeps throwing me this error:
Traceback (most recent call last):
File "C:.........file.py", line 88, in <module>
main()
File "C:.........file.py", line 33, in main
if '-' in done:
TypeError: argument of type 'float' is not iterable
[Finished in 0.2s with exit code 1]
I have looked at others with similiar error but none of them gave me the idea to my current problem.
Still new to python and programming not having a good grasp on this.
Hopefully you can guide me in the right way.
Appreciate your help, folks!
I have done this so far:
def main():
val = '-96000'
flo = float(val)
if '-' in flo:
print('yes')
if __name__ == '__main__':
main()
python
add a comment |
I just want to check the variable contains a negative value but it keeps throwing me this error:
Traceback (most recent call last):
File "C:.........file.py", line 88, in <module>
main()
File "C:.........file.py", line 33, in main
if '-' in done:
TypeError: argument of type 'float' is not iterable
[Finished in 0.2s with exit code 1]
I have looked at others with similiar error but none of them gave me the idea to my current problem.
Still new to python and programming not having a good grasp on this.
Hopefully you can guide me in the right way.
Appreciate your help, folks!
I have done this so far:
def main():
val = '-96000'
flo = float(val)
if '-' in flo:
print('yes')
if __name__ == '__main__':
main()
python
1
You already have a floating point value. Check it before converting or just useif flo < 0:
.
– Matthias
Nov 21 '18 at 16:14
add a comment |
I just want to check the variable contains a negative value but it keeps throwing me this error:
Traceback (most recent call last):
File "C:.........file.py", line 88, in <module>
main()
File "C:.........file.py", line 33, in main
if '-' in done:
TypeError: argument of type 'float' is not iterable
[Finished in 0.2s with exit code 1]
I have looked at others with similiar error but none of them gave me the idea to my current problem.
Still new to python and programming not having a good grasp on this.
Hopefully you can guide me in the right way.
Appreciate your help, folks!
I have done this so far:
def main():
val = '-96000'
flo = float(val)
if '-' in flo:
print('yes')
if __name__ == '__main__':
main()
python
I just want to check the variable contains a negative value but it keeps throwing me this error:
Traceback (most recent call last):
File "C:.........file.py", line 88, in <module>
main()
File "C:.........file.py", line 33, in main
if '-' in done:
TypeError: argument of type 'float' is not iterable
[Finished in 0.2s with exit code 1]
I have looked at others with similiar error but none of them gave me the idea to my current problem.
Still new to python and programming not having a good grasp on this.
Hopefully you can guide me in the right way.
Appreciate your help, folks!
I have done this so far:
def main():
val = '-96000'
flo = float(val)
if '-' in flo:
print('yes')
if __name__ == '__main__':
main()
python
python
asked Nov 21 '18 at 16:11
NiknakNiknak
183212
183212
1
You already have a floating point value. Check it before converting or just useif flo < 0:
.
– Matthias
Nov 21 '18 at 16:14
add a comment |
1
You already have a floating point value. Check it before converting or just useif flo < 0:
.
– Matthias
Nov 21 '18 at 16:14
1
1
You already have a floating point value. Check it before converting or just use
if flo < 0:
.– Matthias
Nov 21 '18 at 16:14
You already have a floating point value. Check it before converting or just use
if flo < 0:
.– Matthias
Nov 21 '18 at 16:14
add a comment |
2 Answers
2
active
oldest
votes
String characters don't exist in float
objects. Just perform a numeric comparison:
if flo < 0:
print('yes')
The keyword in
is used to iterate an iterable object such as a str
instance, so your logic would work with a string:
if '-' in val:
print('yes')
Of course, in the second instance it's wiser to compare against the first character or the start of the string, e.g. val[0] == '-'
or val.startswith('-')
.
add a comment |
Simply remove the cast from str
to float
, so you can control the minus sign is present:
def main():
val = '-96000'
if '-' in val:
print('yes')
if __name__ == '__main__':
main()
Or better, control that the str
actually begins with the minus sign:
if val.startswith('-'):
print('yes')
Or better, still cast to float, then control the value of your data:
def main():
val = '-96000'
flo = float(val)
if flo < 0:
print('yes')
if __name__ == '__main__':
main()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416188%2fcheck-a-float-value-contains-a-minus-sign-hyphen-minus-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
String characters don't exist in float
objects. Just perform a numeric comparison:
if flo < 0:
print('yes')
The keyword in
is used to iterate an iterable object such as a str
instance, so your logic would work with a string:
if '-' in val:
print('yes')
Of course, in the second instance it's wiser to compare against the first character or the start of the string, e.g. val[0] == '-'
or val.startswith('-')
.
add a comment |
String characters don't exist in float
objects. Just perform a numeric comparison:
if flo < 0:
print('yes')
The keyword in
is used to iterate an iterable object such as a str
instance, so your logic would work with a string:
if '-' in val:
print('yes')
Of course, in the second instance it's wiser to compare against the first character or the start of the string, e.g. val[0] == '-'
or val.startswith('-')
.
add a comment |
String characters don't exist in float
objects. Just perform a numeric comparison:
if flo < 0:
print('yes')
The keyword in
is used to iterate an iterable object such as a str
instance, so your logic would work with a string:
if '-' in val:
print('yes')
Of course, in the second instance it's wiser to compare against the first character or the start of the string, e.g. val[0] == '-'
or val.startswith('-')
.
String characters don't exist in float
objects. Just perform a numeric comparison:
if flo < 0:
print('yes')
The keyword in
is used to iterate an iterable object such as a str
instance, so your logic would work with a string:
if '-' in val:
print('yes')
Of course, in the second instance it's wiser to compare against the first character or the start of the string, e.g. val[0] == '-'
or val.startswith('-')
.
answered Nov 21 '18 at 16:13
jppjpp
102k2165115
102k2165115
add a comment |
add a comment |
Simply remove the cast from str
to float
, so you can control the minus sign is present:
def main():
val = '-96000'
if '-' in val:
print('yes')
if __name__ == '__main__':
main()
Or better, control that the str
actually begins with the minus sign:
if val.startswith('-'):
print('yes')
Or better, still cast to float, then control the value of your data:
def main():
val = '-96000'
flo = float(val)
if flo < 0:
print('yes')
if __name__ == '__main__':
main()
add a comment |
Simply remove the cast from str
to float
, so you can control the minus sign is present:
def main():
val = '-96000'
if '-' in val:
print('yes')
if __name__ == '__main__':
main()
Or better, control that the str
actually begins with the minus sign:
if val.startswith('-'):
print('yes')
Or better, still cast to float, then control the value of your data:
def main():
val = '-96000'
flo = float(val)
if flo < 0:
print('yes')
if __name__ == '__main__':
main()
add a comment |
Simply remove the cast from str
to float
, so you can control the minus sign is present:
def main():
val = '-96000'
if '-' in val:
print('yes')
if __name__ == '__main__':
main()
Or better, control that the str
actually begins with the minus sign:
if val.startswith('-'):
print('yes')
Or better, still cast to float, then control the value of your data:
def main():
val = '-96000'
flo = float(val)
if flo < 0:
print('yes')
if __name__ == '__main__':
main()
Simply remove the cast from str
to float
, so you can control the minus sign is present:
def main():
val = '-96000'
if '-' in val:
print('yes')
if __name__ == '__main__':
main()
Or better, control that the str
actually begins with the minus sign:
if val.startswith('-'):
print('yes')
Or better, still cast to float, then control the value of your data:
def main():
val = '-96000'
flo = float(val)
if flo < 0:
print('yes')
if __name__ == '__main__':
main()
answered Nov 21 '18 at 16:15
AntwaneAntwane
8,05332458
8,05332458
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416188%2fcheck-a-float-value-contains-a-minus-sign-hyphen-minus-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You already have a floating point value. Check it before converting or just use
if flo < 0:
.– Matthias
Nov 21 '18 at 16:14