I want to print three different lists, with random choices , all onto one line. How would I do this? This is my code so far:
numbers = list(range(1, 50))
operators = ["+", "-", "x"]
numbers1 = list(range(1,10))
print(random.choice(numbers))
print(random.choice(operators))
print(random.choice(numbers1))
import random
numbers = list(range(1, 50))
operators = ["+", "-", "x"]
numbers1 = list(range(1,10))
print(str(random.choice(numbers)) + random.choice(operators) + str(random.choice(numbers1)))
The numbers are converted to strings first.
You could comma separate the arguments to print
to print a tuple
, or you could use a format string to print the random choices as part of a string:
import random
numbers = list(range(1, 50))
operators = ["+", "-", "x"]
numbers1 = list(range(1,10))
print(random.choice(numbers), random.choice(operators), random.choice(numbers1))
print('{} {} {}'.format(random.choice(numbers), random.choice(operators), random.choice(numbers)))
Sample Output
(18, 'x', 1)
10 + 30
一行输出:
print "number: {0} operator: {1} number1: {2}".format(random.choice(numbers), random.choice(operators), random.choice(numbers1))
您也可以尝试将您需要的所有内容按照您想要的顺序添加到一个变量中,然后打印该变量。
简而言之:
print map(random.choice, (numbers, operators, numbers1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.