Python TypeError:在字符串格式化过程中,并非所有参数都已转换。有关于如何解决这个问题的帮助吗?

Python TypeError:在字符串格式化过程中,并非所有参数都已转换。有关于如何解决这个问题的帮助吗?,python,list,floating-point,Python,List,Floating Point,我在代码竞技场尝试一个有趣的问题,它是: 取3个数字N、D和R(其中N是分子,D是分母,R是小数点后的数字)。假设N=1,D=2,R=20: 所以N/D=1/2=0.50000000000000000000。这里的第20位是0 同样,让N=1,D=2,R=1。所以N/D=1/2=0.5。这里的第一个数字是5 我找到上述问题解决方案的代码是: l=[] N,D,R=input().split() x="%."+str(R)+"f"%(N/D) #Used

我在代码竞技场尝试一个有趣的问题,它是:

取3个数字N、D和R(其中N是分子,D是分母,R是小数点后的数字)。假设N=1,D=2,R=20:

所以N/D=1/2=0.50000000000000000000。这里的第20位是0

同样,让N=1,D=2,R=1。所以N/D=1/2=0.5。这里的第一个数字是5

我找到上述问题解决方案的代码是:

l=[]
N,D,R=input().split()

x="%."+str(R)+"f"%(N/D)  #Used for storing the value of the decimal upto Rth digit

for j in str(x):         #Used to store all the values after the decimal point
    if j==".":
        for k in range(str(x).index(j)+1,len(str(x))):
            l.append(x[k])

print(l[len(l)-1])       #The last index of the list will have the required output
l=[]
但是我不断得到错误
TypeError:在
x=“%.”+str(R)+“f”%(N/D)
行中的字符串格式化过程中,并非所有参数都被转换。我尝试过给出任何随机值,如
x=“%.2f”%(N/D)
,然后它就可以工作了。

这应该可以帮助您:

N,D,R=input().split()

x= f'{float(N)/float(D):.{int(R)}f}'            #Used for storing the value of the decimal upto Rth digit

print(f"Answer = {x}")
print(f"Last digit = {x[-1]}")
输出:

>? 1 2 20
Answer = 0.50000000000000000000
Last digit = 0
>? 1 2 1
Answer = 0.5
Last digit = 5