简体   繁体   English

如何将var同时视为int和string。

[英]How to treat a var as both int and string.

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 + "."

Standard die throwing game, but I'm struggling to print the values for individual die as well as the total. 标准的掷骰子游戏,但是我很难打印单个骰子的值以及总数。 Treating as a string for individual, but then sum leads to concatenation rather than actual sum. 将其视为单个字符串,然后求和会导致串联,而不是实际求和。

Thanks 谢谢

Keep your variables as numbers and let print do the formatting: 将变量保留为数字,并让print执行格式化:

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, "."

Or you could do some formatting using str.format to have more control over the default inter-parameter spacing in the above: 或者,您可以使用str.format进行一些格式化,str.format地控制上述默认参数间的间距:

print "You have rolled a {} and a {}, for a \
total score of {}.".format(first, second, total)

There are two ways to address your problem (and more still!). 有两种方法可以解决您的问题(还有更多方法!)。 Firstly, you need to make sure you keep your integers as type int when adding them together, and then cast them to a string when you print them out. 首先,您需要确保将整数加在一起时将整数保留为int类型,然后在打印出来时将其转换为字符串。

You can do this like the below, using the str() casting method and + concatenation. 您可以使用str()强制转换方法和+级联,如下所示进行操作。

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) + "."

But a handier way is to use the str.format() method to put placeholders in your string, and then let python cast and format the integer values for you. 但是更方便的方法是使用str.format()方法在字符串中放置占位符,然后让python为您str.format()和格式化整数值。 If you have big numbers with 4 or more digits, an advantage of this would be that you can use a string formatting code like "my big number: {0:d,}".format(1000000) to make your string output like "my big number: 1,000,000" , which is much more readable. 如果您有4个或更多数字的大数字,那么这样做的好处是,您可以使用字符串格式代码,例如"my big number: {0:d,}".format(1000000)以使字符串输出类似于"my big number: 1,000,000" ,可读性更高。

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)

You can use casting to change the structure of the var. 您可以使用强制转换来更改var的结构。 You can either use them as strings and for total use this line: 您既可以将它们用作字符串,也可以使用以下代码行:

 total = int(first) + int(second)

or use them as int and cast them to string in the print by using str(first) and str(second) 或将它们用作int并通过使用str(first)和str(second)将它们转换为打印结果中的字符串

Best 最好

print "You have rolled a " + str(first) This would convert the int to a string, hence concatenating it. print "You have rolled a " + str(first)这会将int转换为字符串,从而将其连接起来。

Also, you can do total = int(first) + int(second) to address the first issue. 此外,您可以执行total = int(first) + int(second)解决第一个问题。

You have two solutions: 您有两种解决方案:

  1. Convert the numbers back to int before adding them: 在添加数字之前将其转换回int

     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 + ".") 
  2. Convert the numbers into str before printing them: 在打印数字之前将其转换为str

     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) + ".") 

Either solution will work perfectly fine. 两种解决方案都可以正常工作。

This would also work. 这也将起作用。 Don't convert first and second to str until after you perform sum on them. 在对它们执行求和之前,请勿将firstsecond转换为str Then remember to cast them as str in your print statement. 然后记住在print语句中将它们转换为str

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 + ".")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM