![](/img/trans.png)
[英]Please Help Me with my Code I have no Idea why it isn't working right now
[英]My script isn't working correctly, but I believe that the code is right
我不明白为什么我的脚本不起作用! 有人可以帮忙吗!!! 我正在为我的 CS 课做这件事。 这是代码:
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
break
check(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:
break
check(inches_sum, feet_sum)
print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))
会在没有函数的情况下完成,否则您需要处理返回的值。 还使用 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))
此外,不处理负数,将其作为您的练习:)
一切正常后,您可以尝试将其提取为史蒂夫回答中的函数。
我认为这就是你想要的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.