Udemy课程代码在Sublime中无法运行。我做错了什么?

huangapple go评论69阅读模式
英文:

Udemy course code doesn't work in Sublime. What am i doing wrong?

问题

我正在参加一门关于Python的Udemy课程(我的第一门编程语言),选择的环境是Jupyter。当我尝试在Sublime中编写该代码时,无法获得相同的输出(没有错误)。

def splicer(mystring):
    if len(mystring) % 2 == 0:
        return "Even"
    else:
        return "Odd"

names = ["Andy", "Eve", "Sally"]
list(map(splicer, names))

Udemy课程代码在Sublime中无法运行。我做错了什么?

英文:

I'm taking a Udemy course on Python (my first language) and the environment of choice is Jupyter. When I try to write that code in Sublime, I can't get the same output (there are no errors).
Udemy课程代码在Sublime中无法运行。我做错了什么?

def splicer(mystring):
	if len(mystring)%2 == 0:
		return "Even"
	else:
		return "Odd"

names = ["Andy", "Eve", "Sally"]
list(map(splicer,names))

答案1

得分: 3

你需要打印结果!

print(list(map(splicer,names)))

在Jupyter中,它会自动打印语句的表示,但在编写应用程序时,如果要在屏幕上显示结果,就需要使用print

英文:

You need to print the result!

print(list(map(splicer,names)))

In Jupyter, it automatically prints the representation of a statement, where as when you're writing applications, you need to print if you want the result to be shown on the screen.

答案2

得分: 1

jupyter 充当 Python 解释器,因此如果您输入一个对象,它会自动在下方打印结果。Sublime 是一个文本编辑器,因此它只执行您提供的代码。它正在运行 list(map(splicer,names)),但没有显示对象,因为您没有告诉它要显示。

因此,解释器(jupyter)会实时执行您的 Python 代码并解释(打印到屏幕上)。文本编辑器只执行您的 Python 代码。因此,您需要向对象添加一个打印语句,以使编辑器将对象打印到屏幕上:

print(list(map(splicer,names)))
英文:

jupyter acts as a python interpreter, so if you enter an object it automatically prints the result underneath. Sublime is a text editor, so it is only executing the code you are giving it. It is running list(map(splicer,names)) but it is not displaying the object because you are not telling it to.

So the interpreter (jupyter) is executing your python code in real time and interpreting (printing to screen). The text editor is only executing your python code. Therefore, you need to add a print statement to your object to have the editor print the object to screen:

print(list(map(splicer,names)))

huangapple
  • 本文由 发表于 2020年1月4日 01:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定