my_money = input('How much money do you have? ')
boat_cost = 20 5
if my_money < boat_cost:
print('You can afford the boat hire')
else :
print('You cannot afford the board hire')
CodePudding user response:
Try this code snippet and see the original code problem:
my_money = int( input('How much money do you have? '))
boat_cost = 20 5
if my_money >= boat_cost:
print('You can afford the boat hire')
else :
print('You cannot afford the board hire')
CodePudding user response:
input
is normally a string. In order for this to work you need to convert it to an integrer by using y = int(x)
.
There is also an error in the if
, albeit not a programming one, where it says that they can afford the hire if their money is less than the cost.
The correct code, without fixing the second error, would look like this:
mymoney = int(input('How much money do you have? '))
boatcost = 20 5
if mymoney < boatcost:
print('You can afford the boat hire')
else:
print('You cannot afford the board hire')
CodePudding user response:
The input()
function allows user input in the console.
But that input is a string, you cannot use it as a mathametical operation.
So int()
is a function that takes a string and returns the integer.
you have to use int(input())
when you want to get an input that can only be a number.
so replace:
my_money = input('How much money do you have? ')
with
my_money = int( input('How much money do you have? '))
and your if statement logic is wrong it should be :
if(mymoney >= boatcost):
instead of
if(mymoney < boatcost):
you can afford the boat only when your price is same or higher