I'm quite new to programming and i'm trying to write a formula to work out the capacitance of capacitors - and is what I have below. I then want to convert the value I have (in pico farrads) into micro farrads.

Here's what I have so far:

while True:

    x=float(input("First number: "))
    y=float(input("Second number: "))
    z=float(input("Third number: "))

    x=int(x)
    y=int(y)
    z=int(z)

    print(int(str(x)+str(y))*10**z)

    replay=input("Would you like to calculate again? (yes/no) ")
    print('\n')
    if replay=="no":
        print("...Terminated Program.")
        break

print('\n')

I want to assign the variable 'pf' to the result of line 11, so that I can then multiply the result by *10**-3. I've tried a few things but nothing seems to work. The rest of the script is as it should be and works fine, I just want to be able to add the ability to do what I said above.

Cheers :)

You first want the input as float, then int, later back to str, I think I am not quite sure what you are after.

You assign variables with assignement as you have done already with your x,y and z variables. Can you give your code and error message for what you are trying to do`?

I managed to solve the above BUT have another problem if anyone could look at it:

while True:

    x=float(input("First number: "))
    y=float(input("Second number: "))
    z=float(input("Third number: "))

    x=int(x)
    y=int(y)
    z=int(z)

    pf=(int(str(x)+str(y))*10**z)
    print(pf)
    print(float(pf*10**-3))
    print(float(pf*10**-6))

    replay=input("Would you like to calculate again? (yes/no) ")
    print('\n')
    if replay=="no":
        print("...Terminated Program.")
        break

print('\n')

The problem is shown in the outcome below:

First number: 1
Second number: 0
Third number: 4
100000
100.0
0.09999999999999999
Would you like to calculate again? (yes/no)

As can be seen in line 6, the result I get is supposed to be 0.1 but is instead a long decimal which is, technically equal to 0.1 if it is recurring, but I would rather it simply say 0.1. Anyway, this result is from line 14 in my script - any ideas how to adapt that line in order to solve my problem?

Cheers :)

Use the format method. Here are the different ways to print a floating number centered in a field of length 10, with precision of 2 digits

>>> s = "{val:^10.2e}{val:^10.2E}{val:^10.2f}{val:^10.2F}{val:^10.2g}{val:^10.2G}{val:^10.2n}{val:^10.2%}"
>>> print(s.format(val = 0.1))
 1.00e-01  1.00E-01    0.10      0.10      0.1       0.1       0.1      10.00%  
This article has been dead for over six months. Start a new discussion instead.