[英]Running Python script using an installed 3.4 version in terminal on Mac OS X
Python comes preinstalled on Macs with version 2.7, but I installed python 3.4. Python预先安装在2.7版的Mac上,但我安装了python 3.4。 Let's say I make a simple program...
假设我编写了一个简单的程序...
a = 1
b = 2
print('\nVariable a Is :' , 'One' if (a==1) else 'Not One')
print('Variable a is : ' , 'Even' if (a % 2==0) else 'Odd')
print('\nVariable b is:' , 'One' if(b == 1) else 'Not One')
print('Variable b is', 'Even' if(b % 2 ==0) else 'Odd')
max = a if(a > b) else b
print( '\nGreater Value Is:', max)
If I make this program in TextWrangler, and then run it in terminal, the \\n escape sequence will still show up when it really isn't supposed to. 如果我在TextWrangler中创建此程序,然后在终端中运行它,则\\ n转义序列仍会在确实不应该的情况下显示。 Does this have to do with the version number I am running the script with, if so how can I change to using Python 3.4?
这是否与运行脚本的版本号有关,如果可以,如何更改为使用Python 3.4?
The program is from the book - Python in easy steps 该程序来自本书-轻松完成Python
If you run your file with python yourfile.py
it will run with Python 2.7. 如果您使用
python yourfile.py
运行文件,它将在Python 2.7中运行。 Unless you did something unusual when installing Python 3.4, you need to use python3 yourfile.py
to get that version. 除非在安装Python 3.4时做了一些不寻常的事情,否则您需要使用
python3 yourfile.py
来获取该版本。
The reason \\n
shows up in your output is because you're using the ,
within the print statement, which will create a tuple of the two objects you passed in. In the string representation, the \\n
does not get parsed, but simply shown. 在输出中显示
\\n
的原因是,您在print语句中使用了,
这将创建传入的两个对象的元组。在字符串表示中,不会解析\\n
,而只是对其进行解析如图所示。
Means: 手段:
>>> print( '\nGreater Value Is:', 1)
('\nGreater Value Is:', 1)
As you see, it created ('\\nGreater Value Is:', 1)
. 如您所见,它创建了
('\\nGreater Value Is:', 1)
。
To avoid this, use .format()
or the %
-style syntax: 为避免这种情况,请使用
.format()
或%
-style语法:
>>> print('Greater value is: {0}'.format(23))
Greater value is: 23
Btw. 顺便说一句。 don't use
max
as a variable name, since it's a built-in function in Python. 不要使用
max
作为变量名,因为它是Python中的内置函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.