Posts: 31
Threads: 5
Joined: Jan 2021
Jan-07-2021, 08:48 PM
(This post was last modified: Jan-07-2021, 09:09 PM by Kakha.)
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))
Posts: 6,779
Threads: 20
Joined: Feb 2020
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)) Output: numbers: 333,222,111
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.
Posts: 31
Threads: 5
Joined: Jan 2021
(Jan-07-2021, 09:10 PM)deanhystad Wrote: 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)) Output: numbers: 333,222,111
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. thank you!!!
Posts: 31
Threads: 5
Joined: Jan 2021
(Jan-07-2021, 09:10 PM)deanhystad Wrote: 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)) Output: numbers: 333,222,111
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. 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} ?
Posts: 31
Threads: 5
Joined: Jan 2021
Jan-07-2021, 09:39 PM
(This post was last modified: Jan-07-2021, 09:43 PM by Kakha.)
# 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))
Posts: 6,779
Threads: 20
Joined: Feb 2020
Jan-07-2021, 11:59 PM
(This post was last modified: Jan-08-2021, 12:01 AM by deanhystad.)
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')
^ ^ ^ | | |
| | | | | |
----|---|------------ | |
----|----------------- |
----------------------- Output: 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') Output: 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-Strings
To 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 Output: new Color is: {1},{0},{2}
new Color is: 222,111,333
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)) Output: 0 2 2
new Color is 111,333,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.
Kakha and Skaperen like this post
Posts: 31
Threads: 5
Joined: Jan 2021
(Jan-07-2021, 11:59 PM)deanhystad Wrote: 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')
^ ^ ^ | | |
| | | | | |
----|---|------------ | |
----|----------------- |
----------------------- Output: 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') Output: 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-Strings
To 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 Output: new Color is: {1},{0},{2}
new Color is: 222,111,333
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)) Output: 0 2 2
new Color is 111,333,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.
Thank you you are very good at code !!!
Posts: 8,155
Threads: 160
Joined: Sep 2016
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))?
Posts: 31
Threads: 5
Joined: Jan 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.
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))?
yes this is the correct code
|