Skip to content Skip to sidebar Skip to footer

Loop Program That Loops Inputs

How do you make a loop that program that asks users for how many items they buy, then asks for the price of each item. I've got this: a = input('Enter the number of different ite

Solution 1:

I'll give you the very first part of it - the input() built-in function allows you to solicit and store strings from the user within the command-line.

This will prompt input from the user, printing the string passed to input() before the caret:

input("Please enter something: ")

A string is returned from input() when the user hits enter, which can then be stored in a variable:

user_data = input("Please enter something: ")

Then, what you need to do is fourfold:

  1. Figure out what data types the price and quantity are, and how to ensure that the user data fits those parameters.
  2. Figure out how to combine the quantity and price for any item into a single total price.
  3. Figure out how to add the tax onto that.
  4. Output that value.

Here's a tutorial oninput and output in Python.

Solution 2:

I would store your values in a list.

items = []
for item inrange(1, int(input('how many items do you have? ')+1):
    items.append([
                float(input('what is the price if item {}? '.format(str(item))),
                int(input('how many of item {} do you have? '.format(str(item)))
                ])

the .format does this:

'what is the price of item {}?'.format(str(1))
>> what is the price of item 1?

Post a Comment for "Loop Program That Loops Inputs"