Python3 Error ValueError: invalid literal for int() with base 10: '1.8' - calculation

im trying to make a calculation script with python but im running into this problem:
ValueError: invalid literal for int() with base 10: '1.8'
Im trying to ask a user for an input that is in numbers like 2000 but when the input contains a ',' or '.' it will output that error.
This is the code:
Buiten = int(input("Buiten: "))
Binnen = int(input("Binnen: "))
DikteF = int(input("Dikte: "))
Sortle = int(input("Soortelijk gewicht: "))
inputs = Buiten,Binnen,DikteF,Sortle
Pi = 3.14
R1 = Buiten / 2
R2 = Binnen / 2
UD1 = Pi*R1/1000*R1/1000
UD2 = Pi*R2/1000*R2/1000
Tpv = UD1 - UD2
Ltr = Tpv * DikteF
Srt = Ltr * Sortle
print("")
print("Uitwendige Diameter = ",(round(UD1, 3)),"m²")
print("Inwendige Diameter = ",(round(UD2, 3)),"m²")
print("Product Oppervlak = ",(round(Tpv, 3)), "m²")
print("")
print("Inhoud in L = ",(round(Ltr, 3)),"Liters")
print("Totaal gewicht = ",(round(Srt, 3), "Kilos" ))
print("")
os.system("pause")
while True:
try:
Bereken()
except:
print("Fout: Gebruik alleen cijfers.")
time.sleep(4)
Bereken()

Python returns an error since it is expecting an integer value as string to convert to integer. However, when passing decimals, the attempt to convert to integer fails since they are not integers.
if you are going to insert either integers or decimals, use float
Buiten = float(input("Buiten: "))
Binnen = float(input("Binnen: "))
DikteF = float(input("Dikte: "))
Sortle = float(input("Soortelijk gewicht: "))

Related

How do I make it look like the picture in Lua

This is my code
local level = 5
for i = 1, level do
local text = ""
for j = 1, i do
text = text..""
end
for j = 1, level-i, 1 do
text = text.." "
end
for j = 1+level, level+(level-i) do
text = text.." "
end
for j = 1, level + i-level do
text = text..""
end
print(text)
end
I want the result to be similar to the one in the picture.
Here is what your code looks like with proper formatting.
local level = 5
for i = 1, level do
local text = ""
for j = 1, i do
text = text..""
end
for j = 1, level-i, 1 do
text = text.." "
end
for j = 1+level, level+(level-i) do
text = text.." "
end
for j = 1, level + i-level do
text = text..""
end
print(text)
end
Your current code prints... well... an empty string. You haven't yet added the characters it's to display to be on par with the image.
The amount of characters per row is 9. So you ideally need 9 characters per row. You will also be incrementing the number once per row. The amount of characters per row also increases by 2; one on each side.
We can use the string.rep(string, number) function to duplicate a 'string' 'number' times. You can feed in your current level into that so it generates 1 2 or 3 depending on the line the number of times. Then you have whitespace to worry about. You can use string.rep again along with a bit of distance math to calculate the amount of whitespace you need from what you take up. Then finally throw everything together concatenated trailing with the first string and print.
local levels = 5
local columns = 9
for i=1, levels do
local str = string.rep(i, i)
local padding = columns - (#str * 2) + 1
print(str .. string.rep(" ", padding) .. str)
end

A better way on improving my roman numeral decoder

Quick explanation, I have recently started using codewars to further improve my programming skills and my first challenge was to make a roman numeral decoder, I went through many versions because I wasnt satisfied with what I had, So I am asking if there is an easier way of handling all the patterns that roman numerals have, for example I is 1 but if I is next to another number it takes it away for example V = 5 but IV = 4.
here is my CODE:
function Roman_Numerals_Decoder (roman)
local Dict = {I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000}
local number = 0
local i = 1
while i < #roman + 1 do
local letter = roman:sub(i,i) -- Gets the current character in the string roman
if roman:sub(i,i) == "I" and roman:sub(i + 1,i + 1) ~= "I" and roman:sub(i + 1,i + 1) ~= "" then -- Checks for the I pattern when I exists and next isnt I
number = number + (Dict[roman:sub(i +1,i + 1)] - Dict[roman:sub(i,i)]) -- Taking one away from the next number
i = i + 2 -- Increase the counter
else
number = number + Dict[letter] -- Adds the numbers together if no pattern is found, currently checking only I
i = i + 1
end
end
return number
end
print(Roman_Numerals_Decoder("MXLIX")) -- 1049 = MXLIX , 2008 = MMVIII
at the moment I am trying to get 1049 (MXLIX) to work but I am getting 1069, obviously I am not following a rule and I feel like its more wrong then it should be because usually if its not correct its 1 or 2 numbers wrong.
The algorithm is slightly different: you need to consider subtraction when the previous character has less weight than the next one.
function Roman_Numerals_Decoder (roman)
local Dict = {I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000}
local num = 0
local i = 1
for i=1, #roman-1 do
local letter = roman:sub(i,i) -- Gets the current character in the string roman
local letter_p = roman:sub(i+1,i+1)
if (Dict[letter] < Dict[letter_p]) then
num = num - Dict[letter] -- Taking one away from the next number
print("-",Dict[letter],num)
else
num = num + Dict[letter] -- Adds the numbers together if no pattern is found, currently checking only I
print("+",Dict[letter],num)
end
end
num = num + Dict[roman:sub(-1)];
print("+",Dict[roman:sub(-1)], num)
return num
end
print(Roman_Numerals_Decoder("MXLIX")) -- 1049 = MXLIX , 2008 = MMVIII

undefined reference to `dgemm_' in gfortran in windows subsystem ubuntu

I have the following Fortran code from https://software.intel.com/content/www/us/en/develop/documentation/mkl-tutorial-fortran/top/multiplying-matrices-using-dgemm.html
I am trying to use gfortran complile it (named as dgemm.f90)
! Fortran source code is found in dgemm_example.f
PROGRAM MAIN
IMPLICIT NONE
DOUBLE PRECISION ALPHA, BETA
INTEGER M, K, N, I, J
PARAMETER (M=2000, K=200, N=1000)
DOUBLE PRECISION A(M,K), B(K,N), C(M,N)
PRINT *, "This example computes real matrix C=alpha*A*B+beta*C"
PRINT *, "using Intel(R) MKL function dgemm, where A, B, and C"
PRINT *, "are matrices and alpha and beta are double precision "
PRINT *, "scalars"
PRINT *, ""
PRINT *, "Initializing data for matrix multiplication C=A*B for "
PRINT 10, " matrix A(",M," x",K, ") and matrix B(", K," x", N, ")"
10 FORMAT(a,I5,a,I5,a,I5,a,I5,a)
PRINT *, ""
ALPHA = 1.0
BETA = 0.0
PRINT *, "Intializing matrix data"
PRINT *, ""
DO I = 1, M
DO J = 1, K
A(I,J) = (I-1) * K + J
END DO
END DO
DO I = 1, K
DO J = 1, N
B(I,J) = -((I-1) * N + J)
END DO
END DO
DO I = 1, M
DO J = 1, N
C(I,J) = 0.0
END DO
END DO
PRINT *, "Computing matrix product using Intel(R) MKL DGEMM "
PRINT *, "subroutine"
CALL DGEMM('N','N',M,N,K,ALPHA,A,M,B,K,BETA,C,M)
PRINT *, "Computations completed."
PRINT *, ""
PRINT *, "Top left corner of matrix A:"
PRINT 20, ((A(I,J), J = 1,MIN(K,6)), I = 1,MIN(M,6))
PRINT *, ""
PRINT *, "Top left corner of matrix B:"
PRINT 20, ((B(I,J),J = 1,MIN(N,6)), I = 1,MIN(K,6))
PRINT *, ""
20 FORMAT(6(F12.0,1x))
PRINT *, "Top left corner of matrix C:"
PRINT 30, ((C(I,J), J = 1,MIN(N,6)), I = 1,MIN(M,6))
PRINT *, ""
30 FORMAT(6(ES12.4,1x))
PRINT *, "Example completed."
STOP
END
By gfortran -lblas -llapack dgemm.f90, I got
/tmp/ccUtHQz1.o: In function `MAIN__':
dgemm.f90:(.text+0x794): undefined reference to `dgemm_'
collect2: error: ld returned 1 exit status
I searched that this type of question has been asked time to time, but I haven't found a solution for my case :(
I tried to use python load blas, based on https://software.intel.com/content/www/us/en/develop/articles/using-intel-mkl-in-your-python-programs.html
from ctypes import *
mkl = cdll.LoadLibrary("./anaconda3/lib/libmkl_rt.so")
dgemm = mkl.cblas_dgemm
def print_mat(mat, m, n):
for i in xrange(0,m):
print " ",
for j in xrange(0,n):
print mat[i*n+j],
print
Order = 101 # 101 for row-major, 102 for column major data structures
TransA = 111 # 111 for no transpose, 112 for transpose, and 113 for conjugate transpose
TransB = 111
m = 2
n = 4
k = 3
lda = k
ldb = n
ldc = n
alpha = 1.0
beta = -1.0
amat = c_double * 6
bmat = c_double * 12
cmat = c_double * 8
a = amat(1,2,3, 4,5,6)
b = bmat(0,1,0,1, 1,0,0,1, 1,0,1,0)
c = cmat(5,1,3,3, 11,4,6,9)
print "\nMatrix A ="
print_mat(a,2,3)
print "\nMatrix B ="
print_mat(b,3,4)
print "\nMatrix C ="
print_mat(c,2,4)
print "\nCompute", alpha, "* A * B + ", beta, "* C"
dgemm( c_int(Order), c_int(TransA), c_int(TransB), c_int(m), c_int(n), c_int(k), c_double(alpha), byref(a), c_int(lda), byref(b), c_int(ldb), c_double(beta), byref(c), c_int(ldc))
print_mat(c,2,4)
print
The above code works. Hence, the question may be related to use mkl with gfortran?
You should follow Intel's website to set the compiler flags for gfortran + MKL. Otherwise your will be linking with something else.
https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html

How to get each individual digit of a given number in Basic?

I have one program downloaded from internet and need to get each digit printed out from a three digit number. For example:
Input: 123
Expected Output:
1
2
3
I have 598
Need to Get:
5
9
8
I try using this formula but the problem is when number is with decimal function failed:
FIRST_DIGIT = (number mod 1000) / 100
SECOND_DIGIT = (number mod 100) / 10
THIRD_DIGIT = (number mod 10)
Where number is the above example so here is calulation:
FIRST_DIGIT = (598 mod 1000) / 100 = 5,98 <== FAILED...i need to get 5 but my program shows 0 because i have decimal point
SECOND_DIGIT = (598 mod 100) / 10 = 9,8 <== FAILED...i need to get 9 but my program shows 0 because i have decimal point
THIRD_DIGIT = (598 mod 10) = 8 <== CORRECT...i get from program output number 8 and this digit is correct.
So my question is is there sample or more efficient code that get each digit from number without decimal point? I don't want to use round to round nearest number because sometime it fill failed if number is larger that .5.
Thanks
The simplest solution is to use integer division (\) instead of floating point division (/).
If you replace each one of your examples with the backslash (\) instead of forward slash (/) they will return integer values.
FIRST_DIGIT = (598 mod 1000) \ 100 = 5
SECOND_DIGIT = (598 mod 100) \ 10 = 9
THIRD_DIGIT = (598 mod 10) = 8
You don't have to do any fancy integer calculations as long as you pull it apart from a string:
INPUT X
X$ = STR$(X)
FOR Z = 1 TO LEN(X$)
PRINT MID$(X$, Z, 1)
NEXT
Then, for example, you could act upon each string element:
INPUT X
X$ = STR$(X)
FOR Z = 1 TO LEN(X$)
Q = VAL(MID$(X$, Z, 1))
N = N + 1
PRINT "Digit"; N; " equals"; Q
NEXT
Additionally, you could tear apart the string character by character:
INPUT X
X$ = STR$(X)
FOR Z = 1 TO LEN(X$)
SELECT CASE MID$(X$, Z, 1)
CASE " ", ".", "+", "-", "E", "D"
' special char
CASE ELSE
Q = VAL(MID$(X$, Z, 1))
N = N + 1
PRINT "Digit"; N; " equals"; Q
END SELECT
NEXT
I'm no expert in Basic but looks like you have to convert floating point number to Integer. A quick google search told me that you have to use Int(floating_point_number) to convert float to integer.
So
Int((number mod 100)/ 10)
should probably the one you are looking for.
And, finally, all string elements could be parsed:
INPUT X
X$ = STR$(X)
PRINT X$
FOR Z = 1 TO LEN(X$)
SELECT CASE MID$(X$, Z, 1)
CASE " "
' nul
CASE "E", "D"
Exponent = -1
CASE "."
Decimal = -1
CASE "+"
UnaryPlus = -1
CASE "-"
UnaryNegative = -1
CASE ELSE
Q = VAL(MID$(X$, Z, 1))
N = N + 1
PRINT "Digit"; N; " equals"; Q
END SELECT
NEXT
IF Exponent THEN PRINT "There was an exponent."
IF Decimal THEN PRINT "There was a decimal."
IF UnaryPlus THEN PRINT "There was a plus sign."
IF UnaryNegative THEN PRINT "There was a negative sign."

Split string to numbers in Lua

I've a problem with splitting this:
x = "-301 1021"
to this
x = -301
y = 1021
In C++ this is easier but I really don't know how to do it in Lua. I tried do it like
string1 .. string[i]
#Edit
I've made it like this but now it doesn't get values which are negative
for coordinate in ActualCoords:gmatch("%w+") do
table.insert(coordinates, coordinate)
end
x = coordinates[1];
x = tonumber(x);
z = coordinates[2];
z = tonumber(z);
Assuming your input is regular:
restults = {};
for m in str:gmatch("[^%s]+") do
results[#results+1] = m+0;
end
If input is irregular, meaning you need to verify that its not random things separated by spaces, you need to make a more complex pattern, eg.
"[%deE.-]+"
Try this:
s = "-301 1021"
x,y = s:match("(%S+)%s+(%S+)")
x,y = tonumber(x), tonumber(y)
You can use LPEG for such parsing tasks.
local lpeg = assert(require"lpeg")
local R, S = lpeg.R, lpeg.S
local integer = (S'+-'^-1 * R("09")^1) / tonumber
local space = S(" \t")^0
local x, y = lpeg.match((integer * space)^1, "-301 1021")
print(x,y)
or try this
local s = "-301 1021 -300 1022 -20 -1000"
local coord = {}
for x,y in s:gmatch("(-?%d+).-(-?%d+)") do
coord[#coord+1] = { x=tonumber(x), y=tonumber(y) }
end
print( coord[1].x, coord[1].y)
print( coord[2].x, coord[2].y)
print( coord[3].x, coord[3].y)

Resources