Python Forum

Full Version: A help whit vars and strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Good evening everyone, I have a doubt, I am trying a number of lists to select from each a random value, then I want to format a string depending on the choice of value, this string is a statement of a question eg:
"Juan has 10 dollars, if each apple is worth 2 dollars, how many apple can he buy?"
i have this:
name = ["Carlos","Daniel","Gabriel","Santiago","Aldo"]
product = ["apple", "pie", "soda", "lemon"]
dollars = [50, 75, 100, 125, 150]
cost = [5, 6, 7, 8, 9,10]
statement = "$ has $ dollars. if each $ is worth $ dollars. how many $ apple can he buy?"
statement.replace("$", "%s")
I know how to make the random choice of a list, but i want to give the appropiatre format to the statement var accord the choices, can you bring me a light?, thanks!
Let's suggest you choose random values and you know the order in which the values has to be inserted:
import random
name = ["Carlos","Daniel","Gabriel","Santiago","Aldo"]
product = ["apple", "pie", "soda", "lemon"]
dollars = [50, 75, 100, 125, 150]
cost = [5, 6, 7, 8, 9,10]
pick_name = random.choice(name)
pick_product = random.choice(product)
pick_dollars = random.choice(dollars)
pick_cost = random.choice(cost)
statement = "{0} has {1} dollars. if each {2} is worth {3} dollars. how many {2} can he buy?".format(pick_name, pick_dollars, pick_product, pick_cost)
Output:
Out[8]: 'Santiago has 75 dollars. if each apple is worth 6 dollars. how many apple can he buy?'
This way it does not matter if you names or cost or anything is a string or an integer, the format brings it into the string :)
(May-03-2018, 07:50 AM)ThiefOfTime Wrote: [ -> ]Let's suggest you choose random values and you know the order in which the values has to be inserted:
import random
name = ["Carlos","Daniel","Gabriel","Santiago","Aldo"]
product = ["apple", "pie", "soda", "lemon"]
dollars = [50, 75, 100, 125, 150]
cost = [5, 6, 7, 8, 9,10]
pick_name = random.choice(name)
pick_product = random.choice(product)
pick_dollars = random.choice(dollars)
pick_cost = random.choice(cost)
statement = "{0} has {1} dollars. if each {2} is worth {3} dollars. how many {2} can he buy?".format(pick_name, pick_dollars, pick_product, pick_cost)
Output:
Out[8]: 'Santiago has 75 dollars. if each apple is worth 6 dollars. how many apple can he buy?'
This way it does not matter if you names or cost or anything is a string or an integer, the format brings it into the string :)

Ohh thanks!!, you give a light, now i have this, for x list:
import io,random 
name = ["Carlos","Daniel","Gabriel","Santiago","Aldo"]
product = ["apple", "soda", "pie", "watermelon"]
dollars = [50, 75, 100, 125, 150]
cost = [5, 6, 7, 8, 9,10]
statement = "$ has $ dollars. if each $ is worth $ dollars. how many $ can he buy?"
order = [name,dollars,product,cost,product] #now a list of list to give the format to the statment
choices = [] #An empty list to save the choices
statement = statement.replace("$", "{}") 
for i in order:
    choices.append(random.choice(i)) #select a random value of the list, here we have the problem for example when product is repeated
print(l)
print(statement)
print(statement.format(*choices))
Output:
Carlos has 100 dollars. if each watermelon is worth 10 dollars. how many soda can he buy?
I'm having problems formatting the statement when a variable is repeated, the idea is if for example product had an assignment, not re-evaluate a random choice and no have a idea Think
why do you insist on turning something simple into something unnecessary complex? Look at the example by ThiefOfTime
(May-04-2018, 06:55 AM)buran Wrote: [ -> ]why do you insist on turning something simple into something unnecessary complex? Look at the example by ThiefOfTime

Oh,I understand and appreciate your message, first I wanted to know how to give an appropriate format to the string, nailed it thanks to @ThiefOfTime, later, the idea in the background is to generate from several lists many statements with their respective values ​​and save them in a txt (This part is very easy to do for me) really, I am stuck in the part when I select the variables of these lists and one of the variables I had already chosen, It is not because of making it unnecessarily complex Confused , it is because this is the basis of my software, thank you Big Grin !
No, YOU don't understand.
why you need to construct the template by replacing $ with {}????
your code just complicates the example you have, without adding anything to it
import random
name = ["Carlos","Daniel","Gabriel","Santiago","Aldo"]
product = ["apple", "pie", "soda", "lemon"]
dollars = [50, 75, 100, 125, 150]
cost = [5, 6, 7, 8, 9,10]
order = [name, dollars, product, cost]
for _ in range(10): # create 10 statements
    values = [random.choice(lst) for lst in order]
    statement = "{0} has {1} dollars. if each {2} is worth {3} dollars. how many {2} can he buy?".format(*values)
    print(statement)
Output:
Aldo has 75 dollars. if each lemon is worth 5 dollars. how many lemon can he buy? Carlos has 75 dollars. if each soda is worth 5 dollars. how many soda can he buy? Carlos has 50 dollars. if each soda is worth 10 dollars. how many soda can he buy? Santiago has 150 dollars. if each pie is worth 7 dollars. how many pie can he buy? Gabriel has 100 dollars. if each soda is worth 6 dollars. how many soda can he buy? Santiago has 100 dollars. if each pie is worth 8 dollars. how many pie can he buy? Gabriel has 75 dollars. if each apple is worth 8 dollars. how many apple can he buy? Carlos has 125 dollars. if each soda is worth 10 dollars. how many soda can he buy? Daniel has 50 dollars. if each soda is worth 7 dollars. how many soda can he buy? Santiago has 100 dollars. if each apple is worth 8 dollars. how many apple can he buy?
(May-04-2018, 07:44 AM)dhoyosg Wrote: [ -> ]
(May-04-2018, 06:55 AM)buran Wrote: [ -> ]why do you insist on turning something simple into something unnecessary complex? Look at the example by ThiefOfTime

Oh,I understand and appreciate your message, first I wanted to know how to give an appropriate format to the string, nailed it thanks to @ThiefOfTime, later, the idea in the background is to generate from several lists many statements with their respective values ​​and save them in a txt (This part is very easy to do for me) really, I am stuck in the part when I select the variables of these lists and one of the variables I had already chosen, It is not because of making it unnecessarily complex Confused , it is because this is the basis of my software, thank you Big Grin !

I think I know what you want to do. You want to make several random tasks without the chance of repeating them, right? If so, then remove them from your lists before you are choosing from them again. If I'm wrong on what you want to do, just correct me :D

Though buran is right, the way you try to format the string is too circuitous, @burans last post holds a great example :)
if you want non-repeating statements you can also produce list of unique combinations of values by using set
import random
name = ["Carlos","Daniel","Gabriel","Santiago","Aldo"]
product = ["apple", "pie", "soda", "lemon"]
dollars = [50, 75, 100, 125, 150]
cost = [5, 6, 7, 8, 9,10]
order = [name, dollars, product, cost]
result = set()
while len(result) < 10: # produce 10 unique combinations
    values = (random.choice(lst) for lst in order)
    result.add(values)
# print 10 different statements
for values in result:
    statement = "{0} has {1} dollars. if each {2} is worth {3} dollars. how many {2} can he buy?".format(*values)
    print(statement)
@buran is because my teacher asked that the variables be entered using the sign of $,
@ThiefOfTime, Like @buran example its what i need to do!, Thanks to both of you!, I appreciate your messages, I was able to finish the project thanks to your help!
(May-05-2018, 04:07 AM)dhoyosg Wrote: [ -> ]@buran is because my teacher asked that the variables be entered using the sign of $,
Next time, post your question in Homework section and include the full text/requirments of the assignment in the post
Pages: 1 2