【发布时间】:2014-09-02 05:04:18
【问题描述】:
我的家庭作业很困难,我想我已经接近答案了,但我现在被困住了。基本上我们应该为范围输入一个高整数,为范围输入一个低整数,然后输入一个数字来找出该数字的倍数。虽然我能够得到我输入的任何数字的倍数,但我们应该计算打印的倍数中的偶数和奇数并将它们相加:
这是我目前的代码:
def main():
high = int(input('Enter the high integer for the range: ')) # Enter the high integer
low = int(input('Enter the low integer for the range: ')) # Enter the lower integer
num = int(input('Enter the integer for the multiples: ')) # Enter integer to find multiples
def show_multiples():
# Find the multiples of integer
# and print them on same line
for x in range(high, low, -1):
if (x % num) == 0:
print(x, end=' ')
def isEven(x):
count = 0
total = 0
for count in range():
if (x % 2) == 0:
count = count + 1
else:
count = count + 1
print(count, 'even numbers total to')
print(count, 'odd numbers total to')
isEven(x)
show_multiples()
main()
我离答案很近还是很遥远? python新手这是我第一次在课堂上使用它。
编辑:
Here are the instructions for the homework:
Part 1: Write a program named multiples1.py that generates all multiples of a specified
integer within a specified consecutive range of integers. The main function in the program
should prompt the user to enter the upper and lower integers of the range and the integer
for which multiples will be found. A function named show_multiples should take these three
integers as arguments and use a repetition structure to display (on same line separated by
a space) the multiples in descending order. The show_multiples function is called inside
main. For this program, assume that the user enters range values for which multiples do exist.
SAMPLE RUN
Enter the high integer for the range 100
Enter the low integer for the range 20
Enter the integer for the multiples 15
90 75 60 45 30
Part 2: Make a copy of multiples1.py named multiples2.py. Modify the show_multiples
function so it both counts and totals the even multiples and the odd multiples. The
show_multiples function also prints the counts and sums. See sample run.
SAMPLE RUN
Enter the high integer for the range 100
Enter the low integer for the range 20
Enter the integer for the multiples 15
90 75 60 45 30
3 even numbers total to 180
2 odd numbers total to 120
Part 3: Make another copy of multiples1.py named multiples3.py. Modify the show_multiples
function so that it creates and returns a list consisting of all of the multiples (even
and odd). The show_multiples function should print only "List was created", not the
multiples. Create another function named show_list that takes the list as its sole
argument. The show_list function should output the size of the list, display all of the
list elements, and output the average of the list accurate to two decimal places. See
sample run.
SAMPLE RUN
Enter the high integer for the range 100
Enter the low integer for the range 20
Enter the integer for the multiples 15
List was created
The list has 5 elements.
90 75 60 45 30
Average of multiples is 60.00
【问题讨论】:
-
这取决于,当你运行它时 - 你会得到你期望的答案吗?
-
看起来您对偶数和奇数使用相同的计数?
-
是的,我对偶数和奇数使用相同的计数。我应该为每个做不同的计数吗?是的,当我运行它时,它会打印我输入的数字的倍数。但在那之后它就不起作用了。
-
如果您使用相同的计数,那么您计算的是偶数和奇数的总和,而不是单独的计数。
-
好的,我解决了这个问题,并做了一个 even_count 和一个 odd_count。但它仍然失败,因为 isEven 函数中的 for 循环范围