值包括height = 10,number = 2和bounciness = 6应该达到25.6英尺,但我得到23.2英尺。有人可以帮我理解我的逻辑搞砸了吗?
import math
# User inputs are set up
height = int(input("Enter the height of the ball: "))
number = int(input("Enter the number of bounces of the ball: "))
bounciness = int(input("Enter the height for each successive bounce: "))
count = 0
# Calculation
for count in range(bounciness):
count = (height * bounciness)/100
distance = height + count
bounceSum = number * bounciness
total = count + distance + bounceSum
# Results
print("The ball has traveled a total distance of", total, "feet.")
答案 0 :(得分:1)
如果你看看for循环中的计算,这就是第一次迭代中发生的事情(以及之后的迭代): -
count
= 10 * 6/100 = 0.6
distance
= 10 + 0.6 = 10.6(由于上面的行,此处计数为0.6)
bounceSum
= 2 * 12 = 12
total
= 0.6 + 10.6 + 12 = 23.2
代码中的逻辑问题是变量count
。它有3种不同的定义,它们会覆盖另一种:
count
= 0
count
,范围从0到bounciness-1 count
=(身高* bounciness)/ 100 答案 1 :(得分:0)
我想我修好了......
import math
# User inputs are set up
height = int(input("Hello, please enter the height of the ball: "))
number = int(input("Next, enter the number of bounces of the ball: "))
bounciness = int(input("Now enter the height for each successive bounce: "))
index = 0
# Calculation
for index in range(bounciness):
index = bounciness * 0.6
distance = height + (bounciness * number)
total = index + distance
# Results
print("The ball has traveled a total distance of", total, "feet.")
print("Good bye")