Check a float value contains a minus sign (hyphen-minus) - Python












2















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()









share|improve this question


















  • 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
















2















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()









share|improve this question


















  • 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














2












2








2








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()









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:11









NiknakNiknak

183212




183212








  • 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














  • 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








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












2 Answers
2






active

oldest

votes


















3














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('-').






share|improve this answer































    1














    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()





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      3














      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('-').






      share|improve this answer




























        3














        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('-').






        share|improve this answer


























          3












          3








          3







          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('-').






          share|improve this answer













          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('-').







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 16:13









          jppjpp

          102k2165115




          102k2165115

























              1














              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()





              share|improve this answer




























                1














                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()





                share|improve this answer


























                  1












                  1








                  1







                  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()





                  share|improve this answer













                  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()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 16:15









                  AntwaneAntwane

                  8,05332458




                  8,05332458






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?