![]() |
cannot insert variables {r1}, {r2}, {r3} - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: cannot insert variables {r1}, {r2}, {r3} (/thread-31879.html) |
cannot insert variables {r1}, {r2}, {r3} - Kakha - Jan-07-2021 Please, help import random r1 =(random.randrange(0, 3)) r2 =(random.randrange(0, 3)) r3 =(random.randrange(0, 3)) num1=111 num2=222 num3=333 text1 = "numbers: {r1},{r2},{r3}" # error in this line print(text1.format(num1,num2,num3)) RE: cannot insert variables {r1}, {r2}, {r3} - deanhystad - Jan-07-2021 You are confusing .format with f'. This is your example with corrections, num1=111 num2=222 num3=333 text1 = "numbers: {0},{1},{2}" print(text1.format(num1,num2,num3))The numbers in "numbers: {0},{1},{2}" are index numbers and tell the formatter which argument to print where. This will print the numbers in the opposite order. num1=111 num2=222 num3=333 text1 = "numbers: {2},{1},{0}" print(text1.format(num1,num2,num3)) The code below uses the newest way to format strings, f-Strings.import random r1 =(random.randrange(0, 3)) r2 =(random.randrange(3, 6)) r3 =(random.randrange(6, 9)) print(f'numbers: {r1},{r2},{r3}')When using f-Strings the brackets contain the thing you want to print instead of an index that is used to get the value from the format() function. RE: cannot insert variables {r1}, {r2}, {r3} - Kakha - Jan-07-2021 (Jan-07-2021, 09:10 PM)deanhystad Wrote: You are confusing .format with f'. This is your example with corrections,thank you!!! RE: cannot insert variables {r1}, {r2}, {r3} - Kakha - Jan-07-2021 (Jan-07-2021, 09:10 PM)deanhystad Wrote: You are confusing .format with f'. This is your example with corrections,thank you!!! but I want the random 0 1 2 to show 111, 222, 333 without {1}, {0}, {2}, is this possible using {r1}, {r2}, {r3} ? RE: cannot insert variables {r1}, {r2}, {r3} - Kakha - Jan-07-2021 # random color import random r1 =(random.randrange(0, 3)) r2 =(random.randrange(0, 3)) r3 =(random.randrange(0, 3)) color1=111 #Red color2=222 #Yellow color3=333 #Blue text1 = "new Color is: {r1},{r2},{r3}" print(text1.format(color1,color2,color3)) RE: cannot insert variables {r1}, {r2}, {r3} - deanhystad - Jan-07-2021 You are still mixing up the .format with f-String. If you use .format() then the thing in brackets {0} is an index that says which argument to use in the format string. print("{0} {1} {2}".format('a', 'b', 'c') ^ ^ ^ | | | | | | | | | ----|---|------------ | | ----|----------------- | ----------------------- You can leave the index values out and they are assumed to be 0, 1, … going from left to right.[python]print("{} {} {}".format('a', 'b', 'c') When using format you cannot put anything in the brackets except an integer which will be used as an index. You cannot use a variable that contains the index number. Python does not evaluate the things in the brackets when using .format. That only happens with f-StringsTo do what you want I would change the order of the items and leave the format string alone. # random color import random colors = (111, 222, 333) new_color = random.choices(colors, k=3) text1 = "new Color is: {0},{1},{2}" print(text1.format(*new_color))You can also change the format string, but I think this is really messy and advise against it. # random color import random positions = ('{0}', '{1}', '{2}') random_positions = random.choices(positions, k=3) random_format = "new Color is: {},{},{}".format(*random_positions) print(random_format) # <- So we can see the format string print(random_format.format(111, 222, 333)) # <- Using the format string And if you really want to drive yourself insane you can use .format and f-Strings together like this:# random color import random p0 = random.randint(0,2) p1 = random.randint(0,2) p2 = random.randint(0,2) print(p0, p1, p2) print(f'new Color is {{{p0}}},{{{p1}}},{{{p2}}}'.format(111, 222, 333)) This is truly horrible, but I think it does what you want and is the solution that is closest to your initial attempt. Those triple brackets are required because double brackets "{{" or "}}" is how you treat a bracket as a normal character.
RE: cannot insert variables {r1}, {r2}, {r3} - Kakha - Jan-08-2021 (Jan-07-2021, 11:59 PM)deanhystad Wrote: You are still mixing up the .format with f-String. Thank you you are very good at code !!! RE: cannot insert variables {r1}, {r2}, {r3} - buran - Jan-08-2021 Please, don't quote full posts every time. no need of it and it unnecessarily makes you scroll in order to reach the next post. I don't know if this is intentional or not, but do you realise that you can have repeating "random" positions (i.e. when you do random.randint(0,2) three times in a row, it's possible to get repeating number(s))?
RE: cannot insert variables {r1}, {r2}, {r3} - Kakha - Jan-10-2021 (Jan-08-2021, 07:31 AM)buran Wrote: Please, don't quote full posts every time. no need of it and it unnecessarily makes you scroll in order to reach the next post. yes this is the correct code |