3 Replies - 760 Views - Last Post: 14 November 2012 - 02:07 PM Rate Topic: -----

#1 littlePilot   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 07-March 12

limt the amout of values in a line

Posted 30 October 2012 - 06:11 PM

i want to know how to limt the amount of vales in this bit of code to a value i typed in before in the code

menu = [('Pricot Chicken', 8.50), ('Meat Lovers', 13.50), ('Vegetarian', 13.50), ('Double Cheese', 13.50), ('Hawaiian', 13.50), ('Pepperoni', 8.50), ('Beef and Onion', 8.50), ('Ham and Cheese', 8.50), ('Tomato Margherita', 8.50), ('Classic Italian', 8.50), ('BBQ Chicken and Bacon', 8.50), ('Italian Veg', 8.50)]

while True:
    try:
        num_pizzas= int (input('How meny pizzas do u want to buy?'))
        if num_pizzas in (range (1, 6)):
            break
        elif num_pizzas not in (range (1, 6)):
            pass
    except ValueError:
        pass
    print ("Please enter number under 5")

fmt, nfmt = '{0:<30} {1:>9} {2:>9}'.format, '{0:0.2f}'.format

print('\n'.join('{0:>3}. {1:<30} {2:>9}'.format(i+1, name, nfmt(price)) for i, (name, price) in enumerate(menu)))

print ("What pizzas would you like to order? separated by a space")
order = sorted([ i for i in (int(s)-1 for s in input().split() if s.isdigit()) if i in range(len(menu)) ]) # i want to limt this to the amout num_pizzas


Is This A Good Question/Topic? 0
  • +

Replies To: limt the amout of values in a line

#2 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: limt the amout of values in a line

Posted 31 October 2012 - 02:46 AM

Well. You're asking the user to input his choices on one line, so clearly, you can't limit the amount of numbers he can enter. That gives you two options. One, you can simply take the number of values that you want, and ignore the rest. That's not very nice to the costumer, though.

The other option is to have the user input his choices one at a time, and only ask as many times as you want. I'd do that with a for loop.
for i in range(num_pizzas):

Was This Post Helpful? 0
  • +
  • -

#3 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,735
  • Joined: 13-March 10

Re: limt the amout of values in a line

Posted 31 October 2012 - 03:16 AM

What is the point in having :

if number in range()
elif number not in range()

Use an else clause.
Was This Post Helpful? 0
  • +
  • -

#4 littlePilot   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 07-March 12

Re: limt the amout of values in a line

Posted 14 November 2012 - 02:07 PM

i tryed to use the
for i in range (num_pizzas)
but i just couldn't get it to work like i wanted it too... how do i get that one variable to = so meany different pizzas

menu = [('Pricot Chicken', 8.50), ('Meat Lovers', 13.50), ('Vegetarian', 13.50), ('Double Cheese', 13.50), ('Hawaiian', 13.50), ('Pepperoni', 8.50), ('Beef and Onion', 8.50), ('Ham and Cheese', 8.50), ('Tomato Margherita', 8.50), ('Classic Italian', 8.50), ('BBQ Chicken and Bacon', 8.50), ('Italian Veg', 8.50)]

fmt, nfmt = '{0:<30} {1:>9} {2:>9}'.format, '{0:0.2f}'.format

print('\n'.join('{0:>3}. {1:<30} {2:>9}'.format(i+1, name, nfmt(price)) for i, (name, price) in enumerate(menu)))

print ("What pizzas would you like to order? separated by a space")

for i in range (num_pizza):
       order = sorted([ i for i in (int(s)-1 for s in input().split() if s.isdigit()) if i in range(len(menu)) ])



print (("order for"),name1)
print("Your order:")
print(fmt('Item', 'Price', 'Total'))
total = 0.0
for i in order:
	name, price = menu[i]
	total += price
	print(fmt(name, nfmt(price), nfmt(total)))
print(fmt('Grand Total','', nfmt(total + d_or_p)))
if d_or_p == 'yes':
    print (("your address is"),Address("and your phone number is"),Contact) 


print ("Thanks for using Ethans python code thank you come agian")

next_order = int(input("would u like to cancel or make another order?"))


im just confusing myself more and more lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1