|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:
Write a Python program that uses one or more while loops to calculate:
the sum of odd numbers as output22_odd, and
the sum of even numbers as output22_even in the interval between two given numbers q22_start and q22_stop (inclusive).
Use the following guidelines to develop this program.
For this question, assign the values 21 and 9 to the two variables q22_start and q22_stop, respectively.
Use the two variables output22_odd and output22_even to assign the sum of odd numbers and the sum of even numbers, respectively.
You should handle both the cases q22_start > q22_stop and q22_stop >= q22_start.
Once you have completed your solution, if you have used two while loops, consider how your approach might be optimised to use a single while loop and still handle the guidelines outlined above.
我的答案:
q22_start = 9
q22_stop = 21
output22_odd = 0
output22_even = 0
while q22_stop >= q22_start:
if q22_start%2 != 0:
output22_odd += q22_start
if q22_start%2 == 0:
output22_even += q22_start
q22_start += 1
while q22_stop < q22_start:
if q22_stop%2 != 0:
output22_odd += q22_stop
if q22_start%2 == 0:
output22_even += q22_stop
q22_stop += 1
print(output22_odd)
print(output22_even)
为什么我的出来的值不对?
求大佬指点
|
|