简体   繁体   中英

how would i incorporate .join() in this code for python?

I am trying to get rid of the Parenthesis and commas when i print this code, but cannot find where i need to incorporate the .join() so they can be removed. i really want to figure this out, so if you can please help push me in the right direction that would be great. thank you

def add(x, y):
    return x + y
def subtract(x, y):
    return x - y
def multiply(x, y):
    return x * y
def divide(x, y):
    return x / y

print ("Calculator: ")
print ("add : 1")
print ("subtraction: 2")
print ("multiply: 3")
print ("divide: 4")
foo = input("math solution: ")


num1 = float(raw_input("number 1: "))
num2 = float(raw_input("number 2: "))

if foo == 1:
    print (num1, "+", num2, "=", add(num1, num2)) 
elif foo == 2:
    print (num1, "-", num2, "=", subtract(num1, num2))
elif foo == 3:
    print (num1, "*", num2, "=", multiply(num1, num2))
elif foo == 4:
    print (num1, "/", num2, "=", divide(num1, num2))

You don't need to incorporate .join() in your code. Just remove the parentheses from your print statements, like so:

if foo == 1:
    print num1, "+", num2, "=", add(num1, num2))
elif foo == 2:
    print num1, "-", num2, "=", subtract(num1, num2)
elif foo == 3:
    print num1, "*", num2, "=", multiply(num1, num2)
elif foo == 4:
    print num1, "/", num2, "=", divide(num1, num2)

The reason for this is that in Python 2, print isn't actually called like a function would be; when you add the parens, you're simply packaging your data into tuple s (immutable iterable collections separated by commas). The print statement is then printing those tuples, instead of the data you want printed.

tl;dr remove the parens from print statements, you're accidentally adding data packaging that ruins your print formatting :)

Rather than using str.join , you should take advantage of python's print function . To do this, just put:

from __future__ import print_function

at the top of your file (after your module doctring if it is present). This will work (IIRC) on python 2.6 and later. What's happening in your case is that when you run the code on python2.x,

print (this, that, something_else)

gets interpreted as the print statement and then a tuple to print -- since tuples represent themselves with a parenthesis (and strings represent themselves with quotes), you get unwanted parens and quotes in the output. Enabling the print function with the future statement turns it into a function call which should do the right thing on python2.6+ and python3.x.

You should be thinking about using sting formatting.

In your version of python, you should try

print "%d + %d = %d" % (num1, num2, , add(num1, num2))

and

print "{0} + {1} = {2}".format(num1, num2, add(num1, num2))

The former will work, but the latter (I think) is prettier but only in more recent versions of Python.

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