'getting a user input description for every item in a list
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.
Solution 1:[1]
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+': ')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saket Suraj |
