Python 如何将变量同时视为int和string。

Python 如何将变量同时视为int和string。,python,python-2.x,Python,Python 2.x,标准的掷骰子游戏,但我很难打印出每个骰子的值以及总数。将其视为单个的字符串,然后求和将导致串联,而不是实际求和 谢谢将变量保留为数字,并让打印进行格式设置: def die(): first = str(randint(1, 6)) second = str(randint(1, 6)) total = first + second print "You have rolled a " + first + " and a " + second + ", for a

标准的掷骰子游戏,但我很难打印出每个骰子的值以及总数。将其视为单个的字符串,然后求和将导致串联,而不是实际求和


谢谢

将变量保留为数字,并让
打印
进行格式设置:

def die():
    first = str(randint(1, 6))
    second = str(randint(1, 6))
    total = first + second
    print "You have rolled a " + first + " and a " + second + ", for a total score of " + total + "."
或者,您可以使用
str.format
进行一些格式化,以更好地控制上面的默认参数间间距:

def die():
    first = randint(1, 6)
    second = randint(1, 6)
    total = first + second
    print "You have rolled a", first, "and a", second, ", for a total score of", total, "."

您可以使用强制转换来更改变量的结构。您可以将它们作为字符串使用,也可以使用此行:

print "You have rolled a {} and a {}, for a \
total score of {}.".format(first, second, total)
或者将它们用作int,并使用str(第一个)和str(第二个)将它们转换为字符串


最好的

这也行。不要将
第一个
第二个
转换为
str
,直到对它们执行求和操作。然后记得在
print
语句中将它们转换为
str

 total = int(first) + int(second)

有两种方法可以解决你的问题(甚至更多!)。首先,您需要确保在将整数相加时将它们保持为type
int
,然后在打印时将它们转换为字符串

您可以使用
str()
casting方法和
+
串联,如下所示执行此操作

def die():
    first = randint(1, 6)
    second = randint(1, 6)
    total = str(first + second)
    print ("You have rolled a " + str(first) + " and a " + str(second) + ", for a total score of " + total + ".")
但一种更简便的方法是使用该方法在字符串中放置占位符,然后让python为您转换整数值并设置其格式。如果您有4位或更多数字的大数字,这样做的一个优点是您可以使用字符串格式代码,如
“我的大数字:{0:d,}”。format(1000000)
,使字符串输出如
“我的大数字:1000000”
,这样可读性更高

def die1():
    """Roll and print two dice using concat."""
    first = randint(1, 6) # keep these as integers
    second = randint(1, 6)
    total = first + second # so addition works
    # but now cast to str when printing
    print "You have rolled a " + str(first) + " and a " + str(second) + ", for a total score of " + str(total) + "."
打印“您已滚动一个”+str(第一个)
这将把int转换成一个字符串,从而连接它

还有,你可以
total=int(第一)+int(第二)
解决第一个问题。

您有两种解决方案:

  • 在添加之前,将数字转换回
    int

    def die2():
        """Roll and print two dice using str.format()."""
        first = randint(1, 6)
        second = randint(1, 6)
        total = first + second
        # or use the str.format() method, which does this for you
        print "You have rolled a {0} and a {1}, for a total score of {3}.".format(first, second, total)
    
  • 在打印之前,将数字转换为
    str

    def die():
        first = str(randint(1, 6))
        second = str(randint(1, 6))
        total = str(int(first) + int(second))
        print ("You have rolled a " + first + " and a " + second + ", for a total score of " + total + ".")
    

  • 这两种解决方案都可以很好地工作。

    “您已经滚动了一个”+str(first)
    我建议您仔细研究一下,不要一开始就将int转换为字符串。通常更干净。您好,欢迎来到Stack Overflow,请花点时间浏览一下,了解一下这里的情况(以及您的第一个徽章),阅读如何创建并检查,以增加获得反馈和有用答案的机会。我认为他使用的是Python 2.x。这不会解决连接问题。@Ryan是对的。即使这是Python3,他的答案仍然会因为试图连接不同的类型而失败。有趣的@ChristainDean我用Python3编译了它,它对我有效。它打印了两个值的总和还是连接?因为这就是问题所在。
    def die():
        first = randint(1, 6)
        second = randint(1, 6)
        total = first + second
        print ("You have rolled a " + str(first) + " and a " + str(second) + ", for a total score of " + str(total) + ".")