简体   繁体   中英

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:

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:

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.

You can do this like the below, using the str() casting method and + concatenation.

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

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

Best

print "You have rolled a " + str(first) This would convert the int to a string, hence concatenating it.

Also, you can do total = int(first) + int(second) to address the first issue.

You have two solutions:

  1. Convert the numbers back to int before adding them:

     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:

     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. Then remember to cast them as str in your print statement.

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

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.

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