So, I have this:
nums = []
N = 5
for n in range(N):
numbers = int(input('Please enter an integer: '))
nums.append(numbers)
avg = sum(nums)/5
print(f"The average is:", avg )
for v in nums:
if v > avg:
print("The numbers greater than the average are:\n", (v))
Which outputs something like the following:
Please enter an integer: 5
Please enter an integer: 4
Please enter an integer: 9
Please enter an integer: 7
Please enter an integer: 2
The average is: 5.4
The numbers greater than the average are: 9
The numbers greater than the average are: 7
However I was wondering if someone knew how to make the bottom part show up like this instead:
The numbers greater than the average are:
9 7
try this
nums = []
N = 5
for n in range(N):
numbers = int(input('Please enter an integer: '))
nums.append(numbers)
avg = sum(nums)/5
print(f"The average is:", avg )
g =[]
for v in nums:
if v > avg:
g.append(v)
print("The numbers greater than the average are:\n", *g)
Please enter an integer: 5
Please enter an integer: 4
Please enter an integer: 9
Please enter an integer: 7
Please enter an integer: 2
The average is: 5.4
The numbers greater than the average are:
9 7
Instead of the last part of your code use:
print("The numbers greater than the average are:")
for v in nums:
if v > avg:
print(v, end=" ")
You can just use list comprehension as followings:
...
avg = sum(nums)/5
...
print("The numbers greater than the average are:\n", *[i for i in nums if i > avg])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.