为了账号安全,请及时绑定邮箱和手机立即绑定

我的脚本无法正常工作,但我相信代码是正确的

我的脚本无法正常工作,但我相信代码是正确的

烙印99 2023-08-22 10:12:10
我不明白为什么我的脚本不起作用!有人可以帮忙吗!!!我正在为我的计算机科学课做这个。这是代码:feet1 = int(input('Enter the Feet: '))inches1 = int(input('Enter the Inches: '))feet2 = int(input('Enter the Feet: '))inches2 = int(input('Enter the Inches: '))feet_sum = (feet1 + feet2)inches_sum = (inches1 + inches2)def check(inches_sum, feet_sum):    while True:        if (inches_sum) > 12:            inches_sum -= 12            feet_sum += 1            return feet_sum            return inches_sum            breakcheck(inches_sum, feet_sum)print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))更新:这行得通吗?我非常确定它应该在循环中获取变量并检查英寸是否超过 12,当英寸不超过 12 时,它将中断循环。那有意义吗?feet1 = int(input('Enter the Feet: '))inches1 = int(input('Enter the Inches: '))feet2 = int(input('Enter the Feet: '))inches2 = int(input('Enter the Inches: '))feet_sum = (feet1 + feet2)inches_sum = (inches1 + inches2)def check(inches, feet):    while True:        if (inches_sum) > 12:            inches_sum -= 12            feet_sum += 1        else:            breakcheck(inches_sum, feet_sum)print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))
查看完整描述

2 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

无需函数即可完成此操作,否则您需要处理返回值。还可以使用 while 而不是 if 来使其更加健壮:


feet1 = int(input('Enter the Feet: '))

inches1 = int(input('Enter the Inches: '))

feet2 = int(input('Enter the Feet: '))

inches2 = int(input('Enter the Inches: '))


feet_sum = (feet1 + feet2)

inches_sum = (inches1 + inches2)


while (inches_sum) > 12:

  inches_sum -= 12

  feet_sum += 1


print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))

另外,负数不会被处理,留给你作为练习:)


一切正常后,您可以尝试将其提取为史蒂夫的答案中的函数。


查看完整回答
反对 回复 2023-08-22
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

我想这就是你想要的:


feet1 = int(input('Enter the Feet: '))

inches1 = int(input('Enter the Inches: '))

feet2 = int(input('Enter the Feet: '))

inches2 = int(input('Enter the Inches: '))


feet_sum = (feet1 + feet2)

inches_sum = (inches1 + inches2)


def check(inches_sum, feet_sum):

    while (inches_sum) >= 12:

        inches_sum -= 12

        feet_sum += 1

    return inches_sum, feet_sum


inches_sum, feet_sum = check(inches_sum, feet_sum)


print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))

结果:


Enter the Feet: 1

Enter the Inches: 26

Enter the Feet: 1

Enter the Inches: 26

Feet: 6 Inches: 4


查看完整回答
反对 回复 2023-08-22
  • 2 回答
  • 0 关注
  • 70 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信