xxxxxxxxxx
try:
integer_value = int("some_invalid_value")
# Further code that relies on the converted integer value goes here
print("Converted integer:", integer_value)
except ValueError:
print("Invalid literal for int() with base 10")
xxxxxxxxxx
Enter how many coffee bags are left: 7.4
Traceback (most recent call last):
File "main.py", line 3, in <module>
coffee_bags_as_int = int(coffee_bags)
ValueError: invalid literal for int() with base 10: '7.4'
xxxxxxxxxx
coffee_bags_as_int = int(coffee_bags)
if coffee_bags_as_int > 10:
print("You have enough coffee bags.")
else:
print("You do not have enough coffee bags.")
xxxxxxxxxx
#another way is to check if the entered number isdigit()
#that way you will ensure unhandled exception incase of erroneus data
number= input("What is your weight:")
if number.isdigit():
kilos=int(float(number))
print ("The weight of the person is:" + str(kilos))
else:
print("Error - Please enter a proper weight")
xxxxxxxxxx
#another way is to check if the entered number isdigit()
#that way you will ensure unhandled exception incase of erroneus data
number= input("What is your weight:")
if number.isdigit():
kilos=int(float(number))
print ("The weight of the person is:" + str(kilos))
else:
print("Error - Please enter a proper weight")
xxxxxxxxxx
// You can't put a string value into an INT, you either need to convert it (if you're string is composed of and number)
// Or you need to change the variable content
// How to convert string to int ?
yourVariable = int(stringVariable)
// Convert int to string
yourVariable = str(intVariable)