Home > other >  getting a user input description for every item in a list
getting a user input description for every item in a list

Time:01-27

I want to have my users input a short description of every item in a list that they input the length of.

like this output:

how many items do you want to sell?: 4 (this should be a number between 1 and 25)

enter the name of every item:
1:
2:
3:
4:

(again this list will be determined by what the user inputs at the first question)

enter the price of every item:
name of 1:
name of 2:
name of 3:
name of 4:

my code so far looks like this:

amount = int(input("enter the amount of items you want to sell: "))

while 1 > amount > 25:
    print("Error the number should be between 1 and 25.")
    amount = int(input("enter the amount of items you want to sell: "))

names = []
prices = []

I use lists because that is what is requested of me for the project I am working on.

CodePudding user response:

you should use for loops in cases that you know the length of loop:

amount = int(input('how many items do you want to sell?: '))

item_names = []
item_prices = []

print('enter the name of every item:')
for i in range(1,amount 1):
    item_names.append(input(f'{i}: '))

print('enter the price of every item:')
for i in item_names:
    item_prices.append(input(f'{i}: ')) #you can convert to int or float before appending, if that's what you want

CodePudding user response:

Here is the solution which will take inputs in range between 1 to 25(both excluded). To include, replace > with >= and < with <=

import sys
amount = int(input("enter the amount of items you want to sell: "))
names=[]

if amount>25 or amount<1:
    sys.exit("Invalid Input")
    
print('enter the name of every item:')
for i in range(amount):
    name = input(str(i 1) ': ')
    names.append(name)
    
print('enter the price of every item:')
for name in names:
    input(name ': ')
  •  Tags:  
  • Related