找到成本,但它一直返回错误

发布于 2025-02-07 04:41:19 字数 217 浏览 1 评论 0原文

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')

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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那支青花 2025-02-14 04:41:19

尝试此代码段,查看原始代码问题:

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')

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')
梦醒灬来后我 2025-02-14 04:41:19

输入通常是字符串。为了使其工作,您需要使用y = int(x)将其转换为整合器。

(尽管不是一个编程)中,中也存在错误,它说如果他们的钱低于成本,他们可以负担得起租金。

正确的代码,没有解决第二个错误,就会像这样:

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')

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')
極樂鬼 2025-02-14 04:41:19

input()函数允许在控制台中输入用户。
但是该输入是一个字符串,您不能将其用作数学操作。
因此,int()是一个符合字符串并返回整数的函数。
您必须使用int(input())要获得只能是数字的输入时。

因此,替换:

my_money = input('How much money do you have? ')

with

my_money = int( input('How much money do you have? '))

如果说明逻辑是错误的,则应该是:

if(mymoney >= boatcost):

if(mymoney < boatcost):

当您的价格相同或更高时,您就可以负担得起船

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文