Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: [python] Generating new variables at run time

  1. #1
    Join Date
    Nov 2005
    Beans
    244
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    [python] Generating new variables at run time

    I was just wondering if there was a way to have new variables generated at run time. For example, If I am making a program that tallies the cost of someones pizza order, I could have it say
    Code:
    price_of_pizza_1 = input("Please type the price of the first pizza: ")
    price_of_pizza_2 = input("Please type the price of the second pizza: ")
    
    total = price_of_pizza_1 + price_of_pizza_2
    print "Your total is:", total
    Now that would work fine if I knew how many pizzas were being ordered, but what if some one wants three pizzas?

    Is there a way to have the program generate new variables (eg. price_of_pizza_3,price_of_pizza_4) as they are needed.

    I would like to able to have the program receive inputs from a textbox and generate new pizza variables as the prices are entered and to add the new pizzas to a list. (Using pygtk)

    At the same time I would like their to be a command line version of this as well. (By that I mean, if there is a pygtk method and a separate cli method I would like to know both.)

    Any ideas?

    p.s. The program above is just an example I'm not sure if the code I type would even work in it's current state, I just wrote it to explain what I was trying to do.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [python] Generating new variables at run time

    You can make a list, and append new values as needed.

  3. #3
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [python] Generating new variables at run time

    PHP Code:
    #! /usr/bin/python

    pizza_order = []

    pizza_tally 0
    count 
    True
    while count == True:
        
    pizza raw_input("What type of pizza do you want?")
        if 
    pizza != "":
            
    pizza_order.append(pizza)
        else:
            
    count False

    print "You ordered "len(pizza_order)
    for 
    p in pizza_order:
         print 

    This doesn't tally the price, but gives shows how to add pizzas to a order, and calculate the total amount, and access each one.

    If you want to tally prices, it depends on how the pricing works. If every pizza is the same price, all you need is the length and the ability to multiply.

    I have a feeling what you have in mind doesn't involve pizza's and this was an example.
    Last edited by LaRoza; November 18th, 2007 at 03:09 AM.

  4. #4
    Join Date
    Nov 2005
    Beans
    244
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: [python] Generating new variables at run time

    Quote Originally Posted by LaRoza View Post
    PHP Code:
    #! /usr/bin/python

    pizza_order = []

    pizza_tally 0
    count 
    True
    while count == True:
        
    pizza raw_input("What type of pizza do you want?")
        if 
    pizza != "":
            
    pizza_order.append(pizza)
        else:
            
    count False

    print "You ordered "len(pizza_order)
    for 
    p in pizza_order:
         print 

    This doesn't tally the price, but gives shows how to add pizzas to a order, and calculate the total amount, and access each one.

    If you want to tally prices, it depends on how the pricing works. If every pizza is the same price, all you need is the length and the ability to multiply.

    I have a feeling what you have in mind doesn't involve pizza's and this was an example.
    Ah, thank you. That method looks like it will work, but how would you display the list of pizzas?

    How you have the list output in pygtk. I am not very familar with pygtk so I am planing on learning this.

    No, you are correct I am not making a pizza program, I am trying to work on a gui for lvm. I needed a method to display the various physical volumes that a user has created.

  5. #5
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [python] Generating new variables at run time

    Quote Originally Posted by ihavenoname View Post
    Ah, thank you. That method looks like it will work, but how would you display the list of pizzas?

    How you have the list output in pygtk. I am not very familar with pygtk so I am planing on learning this.
    This script lists the pizzas at the end.

    Code:
    for p in pizza_order:
         print p
    With a list (or tuple), you can iterate through it with a for loop. I don't know much about PyGTK, but making a list should be easy.

  6. #6
    Join Date
    Nov 2005
    Beans
    244
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: [python] Generating new variables at run time

    Quote Originally Posted by LaRoza View Post
    This script lists the pizzas at the end.

    Code:
    for p in pizza_order:
         print p
    With a list (or tuple), you can iterate through it with a for loop. I don't know much about PyGTK, but making a list should be easy.
    Thanks you've been a great help!

  7. #7
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [python] Generating new variables at run time

    Quote Originally Posted by ihavenoname View Post
    Thanks you've been a great help!
    No problem.

  8. #8
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: [python] Generating new variables at run time

    You are making a GUI for LVM? Something like system-config-lvm( which is also made in python)?

  9. #9
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: [python] Generating new variables at run time

    Euuh..I guess I am too late for this since Laroza already answered the question but anyway you can create a loop.. or ask the user at the beginning how many pizzas he wants..use the if conditions...
    and the ask the questions..

  10. #10
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [python] Generating new variables at run time

    Quote Originally Posted by Kadrus View Post
    Euuh..I guess I am too late for this since Laroza already answered the question but anyway you can create a loop.. or ask the user at the beginning how many pizzas he wants..use the if conditions...
    and the ask the questions..
    Trying to increase post count, eh?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •