如何获取变量然后在python

时间:2016-12-23 22:20:18

标签: 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? "))
    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.")

当代码运行时,它会告诉我#" roll",但它会在数字后面加一个空格。我不想要这种行为。 在我发现我将如何更改第二个+骰子卷之后。

想出来,但谢谢!

1 个答案:

答案 0 :(得分:1)

查看print功能的帮助页面。如果给出了一个参数列表,它会打印它们,以sep参数的值分隔,默认情况下这是一个空格。所以:

print("This",1,"is",2,"spaced",3,"out.")
print("This",1,"is",2,"not.",sep="")

表现不同。您需要使用sep=""并手动将自己的尾随或前导空格添加到字符串中,如下所示:

print("You rolled a ", roll, ".", sep="")