Python 3.x 在python中,如何获得一个变量,然后是一个没有空格的字符串

Python 3.x 在python中,如何获得一个变量,然后是一个没有空格的字符串,python-3.x,random,while-loop,Python 3.x,Random,While Loop,所以我做了这个,它能工作,但我有点挑剔,所以我希望它能100%工作。这是密码 import random input_1 = str(input("Do you want to roll a dice? Enter y for yes anything else for no. ")) if input_1 == "y": dicesides = int(input("How many sides do you want your dice to have? ")) dicero

所以我做了这个,它能工作,但我有点挑剔,所以我希望它能100%工作。这是密码

import random
input_1 = str(input("Do you want to roll a dice? Enter y for yes anything else for no. "))
if input_1 == "y":
    dicesides = int(input("How many sides do you want your dice to have? "))
    diceroll = random.randint(1, dicesides)
    print("The dice rolled a",diceroll ".")
    input_2 = str(input("Do you want to roll the dice again? Same keys. "))
    while input_2 == "y":
        print(random.randint(1, dicesides))
        input_2 = str(input("Do you want to roll the dice again? Same keys. "))
    else:
        print("Okay, goodbye now.")

else:
    print("Okay, goodbye now.")
当代码运行时,它告诉我它“滚动”的数字,但它在数字后面加了一个空格。我不想要这种行为。 在我找到如何将第二个+骰子掷骰更改为它之后


我知道了,但是谢谢

查看
打印功能的帮助页面。如果给它一个参数列表,它将打印它们,并用
sep
参数的值分隔,默认情况下,该值是一个空格。因此:

print("This",1,"is",2,"spaced",3,"out.")
print("This",1,"is",2,"not.",sep="")
会有不同的表现。您需要使用
sep=“”
并将自己的尾随空格或前导空格手动添加到字符串中,如下所示:

print("You rolled a ", roll, ".", sep="")
print(“骰子滚了一个”,骰子滚“)
是无效语法。我想你需要
打印(“骰子滚了一个{}.”格式(骰子滚))