Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with a while loop
#1
Hey, i am having a problem with a while loop and i am a beginner in python. How could i do a while loop so that python understands it this way : While n is not an integer, add 1 to the value of m(so in that case m would become 2) and then recalculate everything.
Thanks!

R=float(7)
r=float(5)
m=float(1)
n=(r/(R-r))*m

if n==int:
    print('Voici la valeur de n désirée',n)
else:
    while n!=int:
        m+=1
    print("Voici les valeurs m et n (m,n)",(m,n))
Reply
#2
n is never going to be an int. If it's a float, it stays a float, it never changes to an int.

The way to check for this is to see if it is close to it's integer version:

if abs(n - int(n)) < 0.00000001:
You don't want to check if the difference is zero, because there may be floating point errors. But those should be really small, so that's why you test that the difference is less than a small number.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Also, if you ever do want to check if something is an int, use:

if isinstance(n, int):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Problem Benno2805 1 587 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,578 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Infinite loop problem Zirconyl 5 3,003 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,184 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Python loop problem Kristenl2784 11 5,109 Jun-18-2020, 07:22 PM
Last Post: buran
  Problem with append list in loop michaelko03 0 1,688 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  problem with for loop using integers python_germ 5 3,008 Aug-31-2019, 11:42 AM
Last Post: jefsummers
  problem in loop roseojha 3 2,306 Aug-26-2019, 09:03 AM
Last Post: perfringo
  Nested while loop problem + turtle DreamingInsanity 3 2,981 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Problem Passing Arguement to do loop stephenmolnar 10 4,866 May-13-2019, 02:56 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020