3.6 Code Practice Question:
Write a program to input 6 numbers. After each number is input, print the biggest of the number entered so far:
Answer:
nums = []
biggest = 0
for i in range(6):
num = int(input("Number: "))
nums.append(num)
if(i == 0):
print("Largest: "+str(nums[0]))
biggest = nums[0]
else:
if nums[i]>biggest:
print("Largest: "+str(nums[i]))
biggest = nums[i]
else:
print("Largest: "+str(biggest))
Explanation:
This question is answered using python
This line declares an empty list
nums = []
This line initalizes the biggest input to 0
biggest = 0
This iteration is perfoemed for 6 inputs
for i in range(6):
This prompts user for input
num = int(input("Number: "))
This appends user input to the list created
nums.append(num)
For the first input, the program prints the first input as the largest. This is implemented in the following if condition
<em> if(i == 0):</em>
<em> print("Largest: "+str(nums[0]))</em>
<em> biggest = nums[0]</em>
For subsequent inputs
else:
This checks for the largest input and prints the largest input so far
<em> if nums[i]>biggest:</em>
<em> print("Largest: "+str(nums[i]))</em>
<em> biggest = nums[i]</em>
<em> else:</em>
<em> print("Largest: "+str(biggest))</em>
<em />