Reputation: 11
So, this is the output that I want from the input:
Input:
5
4
3
7
6
Output:
P[1]5
P[2]4
P[3]3
P[4]7
P[5]6
Lowest Number = 3
Highest Number = 7
But I got this with my code
[P1] 4
[P2] 4
[P3] 4
[P4] 4
[P5] 4
Lowest Number = 3
Highest Number = 7
This is my code:
X = []
for i in range (0,5):
X.append(int(input()))
for j in range (0,5):
print("[P"+str(j+1)+"]", i)
print("Lowest Number = ",min(X))
print("Highest Number = ",max(X))
I got a few questions:
Where do all the '4's come from?
Is there a way to remove the space between [P1] and 4 so it should be [P1]4 (from the wrong output) or [P1]5 (from the right output)?
Thank you.
Upvotes: 0
Views: 154
Reputation: 118
ad 1. You printed variable i
, which has set to 4 as the last number of the first range. I recommand to use enumerate
function and iterate trought the list X
, which is a better practice, because you can change a length of X
.
ad 2. Use a better way to control your output (.format, f-string since Python 3.6).
Here's example about .format:
X = []
for i in range (0,5):
X.append(int(input()))
for no, number in enumerate(X, start=1):
print("[P{0}]{1}".format(no, number))
print("Highest Number = {0}".format(max(X)))
print("Lowest Number = {0}".format(min(X)))
A here's example about f-string:
X = []
for i in range (0,5):
X.append(int(input()))
for no, number in enumerate(X, start=1):
print(f"[P{no}]{number}")
print(f"Highest Number = {max(X)}")
print(f"Lowest Number = {min(X)}")
For example, here's a quick review of formatting: https://realpython.com/python-string-formatting/
Upvotes: 1
Reputation: 229
Well, according to your code
X = []
for i in range (0,5):
X.append(int(input()))
for j in range (0,5):
print("[P"+str(j+1)+"]", i)#there is the problem
print("Highest Number = ",max(X))
print("Lowest Number = ",min(X))
you are calling i
instead of X[j]
Upvotes: 0