I have these lines of code:
Num1 = random.randint(1, 10)
Num2 = random.randint(1, 10)
Num3 = random.randint(1, 10)
Num4 = random.randint(1, 10)
Number = Num1 + Num2 + Num3 + Num4
print(Number)
I want to make it so that instead of it adding the Nums together and printing a number between 4 and 40, it prints it like this: "1234".
You need to convert each value to a string and concatenate them. One simple way is
number = "%d%d%d%d" % (Num1, Num2, Num3, Num4)
您可以打印变量并定义分隔符,如下所示:
print(Num1, Num2, Num3, Num4, sep="")
print ''.join([str(x) for x in [1,2,3,4]])
另一种方法是使用str.format
。
number = "{}{}{}{}".format(Num1, Num2,Num3, Num4)
You can encapsulate the statement with str():
Num1 = str(random.randint(1, 10))
Num2 = str(random.randint(1, 10))
Num3 = str(random.randint(1, 10))
Num4 = str(random.randint(1, 10))
Number = Num1 + Num2 + Num3 + Num4
print(Number)
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.