Error ValueError: invalid literal for int () with base 10: '6.56' in python
Https://pastebin.com/dkKZx3PS - code
import math
from colorama import init
from colorama import Fore, Back, Style
init()
print( Fore.BLACK )
print( Back.RED )
print('Добро пожаловать в дебильный калькулятор V5.3!')
print('После вопроса пробелы ставить не нужно, а сразу писать что у Вас спрашивают!')
print( Back.CYAN )
print('+ - сложение')
print('- - вычитание')
print('* - умножение')
print('/ - деление')
print('** - возведение в степень')
print('% - деление по модулю')
print('S - пропустить и перейти к округлению и числу ПИ')
print( Back.GREEN )
what = input( "Что делаем? (+, -, *, /, **, %): " )
print( Back.YELLOW)
if what == "+":
a = float( input("Введи первое число: ") )
b = float( input("Введи второе число: ") )
c = a + b
print("Результат: " + str(c))
if what == "*":
a = float( input("Введи первое число: ") )
b = float( input("Введи второе число: ") )
c = a * b
print("Результат: " + str(c))
if what == "/":
a = float( input("Введи первое число: ") )
b = float( input("Введи второе число: ") )
c = a / b
print("Результат: " + str(c))
if what == "**":
a = float( input("Введи первое число: ") )
b = float( input("Введи второе число: ") )
c = a ** b
print("Результат: " + str(c))
if what == "%":
a = float( input("Введи первое число: ") )
b = float( input("Введи второе число: ") )
c = a % b
print("Результат: " + str(c))
if what == "-":
a = float( input("Введи первое число: ") )
b = float( input("Введи второе число: ") )
c = a - b
print("Результат: " + str(c))
print( Back.GREEN )
if what == "S":
print('1 - обычное округление')
print('2 - округление к меньшему числу')
print('3 - округление к большему числу')
print('4 - число ПИ')
vibor2 = input('Ваш выбор? (1, 2, 3, 4): ')
if vibor2 == "1":
d = input('Число для округления?: ' )
k = int(d)
print( 'Результат: ' + round(k) )
if vibor2 == "2":
d = input('Число для округления?: ' )
k = int(d)
print( 'Результат: ' + math.floor(d) )
if vibor2 == "3":
d = input('Число для округления?: ')
k = int(d)
print( 'Результат: ' + math.ceil(d) )
if vibor2 == "4":
print('Число ПИ: ' + str(math.pi) )
print()
I tried different options: Changed to float, int, bool. Then I added "k = int(d)" and also changed it. The error just changed, but the program did not start normally. I left the code with "k = int(d)", but if anything I will remove it. Only three rounding doesn't work, everything else works. Please help me.
3 answers
Instead of the int()
function in the{[10] commands]}
k = int(d)
Apply the function float()
:
k = float(d)
(as you did for the variables a
and b
above.)
Explanation:
It is not possible to use the expressionint(k)
to convert the string k
when it contains something other than an integer (with possible spaces before and after, but no spaces inside the number).
In some other languages, this is acceptable, but not in Python.
Also, you still need in the variable k
decimal number, because you want to round it with different images (which will give the same result for integers).
int('34.4')
it will not work, because you need to do, for example, so: int(float('34.4'))
In your case, if you want to round up, then you do, say,
if vibor2 == "1":
d = input('Число для округления?: ' )
k = float(d)
print( 'Результат: ' + round(k) )
Like this:
import numpy
d = input('Число для округления?: ' )
k = numpy.float64(d)