简体   繁体   English

我如何将.join()合并到python的此代码中?

[英]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. 我在打印此代码时试图摆脱括号和逗号,但找不到我需要在哪里合并.join()以便将其删除的地方。 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. 您无需在代码中合并.join() Just remove the parentheses from your print statements, like so: 只需从print语句中删除括号即可,如下所示:

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; 原因是在Python 2中,实际上并不像函数那样调用print when you add the parens, you're simply packaging your data into tuple s (immutable iterable collections separated by commas). 添加括号时,您只是将数据打包到tuple (用逗号分隔的不可变的可迭代集合)。 The print statement is then printing those tuples, instead of the data you want printed. 然后, print语句将打印这些元组,而不是要打印的数据。

tl;dr remove the parens from print statements, you're accidentally adding data packaging that ruins your print formatting :) tl; dr从print语句中删除括号,您无意中添加了破坏打印格式的数据包装:)

Rather than using str.join , you should take advantage of python's print function . 而不是使用str.join ,您应该利用python的print 函数 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. 这将在python 2.6及更高版本上起作用(IIRC)。 What's happening in your case is that when you run the code on python2.x, 您遇到的情况是,当您在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. 被解释为print 语句 ,然后解释为要printtuple -因为元组用括号表示自己(字符串用引号表示自己),所以输出中会出现不必要的括号和引号。 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. 使用future语句启用print函数会将其转换为一个函数调用,该函数应该在python2.6 +和python3.x上做正确的事情。

You should be thinking about using sting formatting. 您应该考虑使用sting格式。

In your version of python, you should try 在您的python版本中,您应该尝试

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. 前者可以工作,但后者(我认为)更漂亮,但仅适用于最新版本的Python。

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

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