问题描述
我不知道为什么我的脚本不起作用!有人可以帮忙!!!我正在为我的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,inches_sum))
解决方法
在没有函数的情况下会这样做,否则您需要处理返回的值。还可以使用while来代替它,以使其更强大:
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))
此外,负数也不会处理,留给您练习:)
一切正常后,您可以尝试像Steve的答案一样将其提取为函数。
,我想这就是你想要的:
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