Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - 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 items you are purchasing")

  for i in range(1,a+1):
      b = input("Enter the price of item number")
       c = input("Enter the quantity of this item")

However, I don't know how to make the input statements say "enter number price 1" and for the second loop "enter the price of item 2"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...