Jump to content

Python Program not reasigning variable when if statement run

Go to solution Solved by fizzlesticks,

The problem is in the line

pi = int(4*(withinone/loop))

withinone and loop are both integers so it does an integer division, which in your case is resulting in 0. To do a floating point division you'll need to cast one or both of those variables to a float. You'll also probably want to remove the int cast.

Hello all, I have this Python program (bellow) and its not doing what its supposed to >:( wondering if anyone can help???

 

loop = int(input('How many coordinates do you want? '))
looped = 0
withinone = int(0)
for x in range(0,loop):
    looped = looped + 1
    x = float(random.random())
    y = float(random.random())
    dfo = float((x**2+y**2)**0.5)
    print dfo
    if dfo <= 1:
        withinone = withinone + int(1)
        print "less than or equal to 1"
        print withinone
    elif dfo > 1:
        print "more than 1"
    else:
        print "error"
    xstring = str(x)
    ystring = str(y)
    dfostring = str(dfo)
    print "Coordinate Set: ",looped
    print "x = ",xstring
    print "y = ",ystring
    print "Dist. from Origin = ",dfostring
    csv = open('coordinates.csv','a')
    csv.write(xstring)
    csv.write(',')
    csv.write(ystring)
    csv.write(',')
    csv.write(dfostring)
    csv.write('\n')
pi = int(4*(withinone/loop))
pistring = str(pi)
print "pi approximation = ",pistring
csv.write('\n')
csv.write(pistring)

 

In the if statement "if dfo <= 1:" there is the variable "withinone" which is reassigned as "withinone + 1". This is then divided by the variable loop further down the program. This sum is saved as the integer variable "pi". I then convert this integer variable ("pi") to a string variable "pistring". When asked to print variable "pistring" it prints as "0". Its should not be 0. 

 

I am completely unsure what is wrong with my code so if anyone can help that would be great!

 

Sidenote: try and keep solutions simple as I am compleatly new to python and programming in general, thanks!

Link to post
Share on other sites

The problem is in the line

pi = int(4*(withinone/loop))

withinone and loop are both integers so it does an integer division, which in your case is resulting in 0. To do a floating point division you'll need to cast one or both of those variables to a float. You'll also probably want to remove the int cast.

1474412270.2748842

Link to post
Share on other sites

6 minutes ago, fizzlesticks said:

The problem is in the line


pi = int(4*(withinone/loop))

withinone and loop are both integers so it does an integer division, which in your case is resulting in 0. To do a floating point division you'll need to cast one or both of those variables to a float. You'll also probably want to remove the int cast.

 

That has worked! Thanks! :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×