我有一个很快的问题,我一直想弄清楚一段时间。我正在编写一个代码,该代码接受输入的数字范围(高和低),然后使用输入的数字来确定该范围内是否存在该数字的倍数。然后它将总计奇数和偶数的总和,并添加有多少。我已经准备好了所有的东西,但我的问题是我不能分开这条线" 90 75 60 45 30"从另一条线" 3个偶数总计到180"。我确定它很简单,但我无法弄清楚。有人能指出我正确的方向吗?提前感谢您的时间和考虑。
以下代码返回:
Number of high range?: 100
Number of low range?: 20
Multiple to find?: 15
90 75 60 45 30 3 even numbers total to 180
2 odd numbers total to 120
代码:
def main():
x = int(input('Number of high range?: '))
y = int(input('Number of low range?: '))
z = int(input('Multiple to find?: '))
show_multiples(x,y,z)
def show_multiples(x,y,z):
for a in range(x,y,-1):
if a % z == 0:
print (a,end=' ')
even_count = 0
even_sum = 0
odd_count = 0
odd_sum = 0
for num in range(x,y,-1):
if num % z == 0 and num % 2 == 0:
even_count += 1
even_sum += num
for number in range(x,y,-1):
if number % z == 0 and number % 2 == 1:
odd_count += 1
odd_sum += number
print(even_count,'even numbers total to',even_sum)
print(odd_count,'odd numbers total to',odd_sum)
main()
答案 0 :(得分:4)
print('\n', even_count, ' even numbers total to ', even_sum, sep='')
应该这样做。只需手动在某个地方添加新行
答案 1 :(得分:0)
问题的最小例子:
>>> def test1():
for _ in range(3):
print("foo", end=" ")
print("bar")
>>> test1()
foo foo foo bar # still using end=" " from inside the loop
一个解决方案的最小示例:
>>> def test2():
for _ in range(3):
print("foo", end=" ")
print() # empty print to get the default end="\n" back
print("bar")
>>> test2()
foo foo foo
bar
这个空的print
可以位于for
循环的末尾,print
个别号码和print(even_count, ...
,例如:
...
odd_sum += number
print()
print(even_count, 'even numbers total to', even_sum)
答案 2 :(得分:0)
定义 main():
public IEumerable<int> MyMethod(){
Task<int> task = GetNextAsync();//Prepare for next return
yield return 0;
yield return await task;
}
def show_multiples(x,y,z):
x = int(input('Number of high range?: '))
y = int(input('Number of low range?: '))
z = int(input('Multiple to find?: '))
print("The numbers are as follows:")
show_multiples(x,y,z)
main()