基本上,我真的被卡住了.
我有这样的文字,我需要这样做: *print prompt file_again = STDIN.gets.chomp()txt_again = File.open(file_again)puts txt_again.read()*
并且基本上从我的控制台上打印的.txt文件中获取文本!
直接从irb使用File.open(),但后来尝试:
File.open("ex15_sample.txt")
Run Code Online (Sandbox Code Playgroud)
我认为它打开但我仍然无处可去.我的意思是,它没有标记为变量,我无法打印它.
如果我将使用:
txt = File.open("ex15_sample.txt")
Run Code Online (Sandbox Code Playgroud)
我首先会得到一些错误,所以我以后不能使用print txt.
练习来自http://ruby.learncodethehardway.org/book/ex15.html,我正在尝试做一些可选的东西,所以我不会像我之前做过的代码学校初学者课程一样无处可去.
def gei(a, b):
'''
a, b: only positive integers! If you don't, we will make you. Max 1337
For extracting the juice of the numbers in the form of a common divider
'''
#Not Ruby, no to_i, but int()
smallest = int(abs(min(a, b)))
biggest = int(abs(max(a, b)))
print "You inputed: ", smallest, " and ", biggest, " their order doesn't matter."
print "Do you want to see guess numbers? Type 'yes', if you do! "
selection = raw_input()
print
print …Run Code Online (Sandbox Code Playgroud) 所以基本上,假设您定义了一个函数,然后在函数之后在()括号中写入一些东西.如果我写函数(同性恋),它最终会被'错误名称未定义'Python2.73
def lenRecur(aStr):
'''
aStr: a string
returns: int, the length of aStr
'''
print type(aStr)
return
def lenRecur2(aStr):
'''
aStr: a string
returns: int, the length of aStr
'''
s = str(aStr)
print type(s)
return
Run Code Online (Sandbox Code Playgroud)
因此,当我输入lenRecur(同性恋)或lenRecur2(同性恋)时,无论我做什么,它都会以错误结束.有没有办法避免它?不使用""引号.