Basic python learning:
I am asked to write a program that makes a list of 15 integer entries (size) the user inputs. For each input, the program should give the number of integers remaining to input. In the end, the program should list the 15 entries, and tell the user he is done. With the code below, I get the list and the final report, but can't really figure out how to count down, print the remaining entries, and tell the user he is done.
my_list = []
for _ in range(15):
try:
my_list.append(int(input('Enter size: ')))
except ValueError:
print('The provided value must be an integer')
print(my_list)
CodePudding user response:
One thing to figure out is "what to do if the user inputs something that is not a valid integer?" You can solve that by keeping asking the user for an input until it's a valid number.
You can do that using a while
loop with a boolean variable:
good_int = False
while not good_int:
try:
num = int(input(f'Enter an integer: '))
good_int = True
except ValueError:
print('I said int, gorramit!!')
Now that we have that, there is a lot of approaches you can take here.
One of the interesting things a for
loop can do is count backwards.
You can try:
my_list = []
for i in range(15, 0, -1):
good_int = False
while not good_int:
try:
my_list.append(int(input(f'Enter size for iteration {i}: ')))
good_int = True
except ValueError:
print('The provided value must be an integer')
print("Aight, you're done. This is the list:")
print(my_list)
Or... probably what is better, use a variable to store the number of tries (15) and don't consider a "valid try" until the user inputted a valid integer.
my_list = []
tries = 15
while tries > 0:
try:
my_list.append(int(input(f'Enter size for iteration {tries}: ')))
tries = tries - 1 # Or just tries -= 1
except ValueError:
print('The provided value must be an integer')
print("Aight, you're done. This is the list:")
print(my_list)
CodePudding user response:
Since you don't want the loop to repeat a fixed number of times, but instead need it to repeat until you have 15 values, you should consider using a while
instead of a for
.
my_list = []
while len(my_list) < 15:
try:
my_list.append(int(input('Enter size: ')))
except ValueError:
print('The provided value must be an integer')
print(f'You are done: {my_list}')
It may be a good idea to inform the user how many values they still have to enter:
my_list = []
while len(my_list) < 15:
try:
my_list.append(int(input(f'Enter size ({15 - len(my_list)} left): ')))
except ValueError:
print('The provided value must be an integer')
print(f'You are done: {my_list}')
CodePudding user response:
You're going to need to use a counter variable, which simply keeps track of the amount of numbers left for the program to receive. You will print it out after every input, and then subtract 1 from the variable. I added a check to make sure that we don't print out "You have to input 0 more numbers" since that isn't what we really want to do.
my_list = []
numbers_left = 15
for _ in range(15):
try:
my_list.append(int(input('Enter size: ')))
numbers_left = numbers_left - 1
if numbers_left != 0:
print("You have to input " str(numbers_left) " numbers to complete the program.")
except ValueError:
print('The provided value must be an integer')
print(my_list)
CodePudding user response:
You can print with f strings and the deincrement the number for each for loop. I am not sure about the try and for loop isn't this better?
EDIT: Changed the for loop and gives errors on string vals.
my_list=[]
len_val=15
val=0
while val<len_val:
try:
my_list.append(int(input("Enter size:")))
val =1
print(f"{(len_val-val)}to go!")
except ValueError:
print("Enter an integer")
print(len(my_list))
CodePudding user response:
range(14, 0, -1)
will do the job for you.
my_list = []
for i in range(14, 0, -1):
try:
my_list.append(int(input('Enter size: ')))
print(f'{i} to go')
except ValueError:
print('The provided value must be an integer')
print(my_list)
If you have ValueError
during the execution. I would suggest using a while loop. Like this
my_list = []
size = 15
while len(my_list) < size:
try:
my_list.append(int(input(f'Enter size : ')))
print(f'{size-len(my_list)} to go')
except ValueError:
print('The provided value must be an integer')
print(f'You are done: {my_list}')