简体   繁体   English

在Mac OS X上的终端中使用已安装的3.4版本运行Python脚本

[英]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.

相关问题 为Python 3.4安装pip - Mac OS X. - Installing pip for Python 3.4 - Mac OS X 在 Mac OS X 上完全清除终端而不退出 Python 脚本 - Fully clear terminal on Mac OS X without exiting Python script 安装Anaconda3会将Mac OS X的默认Python版本更改为3.4吗? - Will installing Anaconda3 change Mac OS X default Python version to 3.4? 我怀疑我在Mac OS X 10.6.3上安装了多个版本的Python 2.6; 如何设置应该启动哪个终端? - I suspect I have multiple version of Python 2.6 installed on Mac OS X 10.6.3; how do I set which one Terminal should launch? Mac OS X终端中的Python unicode - Python unicode in Mac os X terminal 在Mac Os X Mavericks(10.9)上为Python 3.4安装Pillow - Install Pillow for Python 3.4 on Mac Os X Mavericks (10.9) OS X:如何链接更新的Python版本(3.4到3.6) - OS X: How to link updated Python version (3.4 to 3.6) 在Mac OS X中运行没有终端或停靠项的python脚本和已编译的c代码 - Run a python script and a compiled c code without terminal or dock item in Mac OS X Python在OS X终端中不执行脚本 - Python does not execute script in OS X terminal Mac Yosemite os变量可在bash Python解释器中运行,可在终端中运行脚本,但无法在Sublime Text 2文本编辑器中运行 - Mac Yosemite os variable works in bash Python interpreter, running the script in terminal, but not in Sublime Text 2 text editor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM