小编5br*_*mzh的帖子

如何在irb控制台中使用open()从.txt文件中打印出基本文本?#红宝石

基本上,我真的被卡住了.

我有这样的文字,我需要这样做: *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,我正在尝试做一些可选的东西,所以我不会像我之前做过的代码学校初学者课程一样无处可去.

ruby cmd irb

2
推荐指数
1
解决办法
6862
查看次数

找出最大的公约数

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)

python python-2.7

1
推荐指数
1
解决办法
5325
查看次数

如果不是,如何使函数输入成为一个字符串?

所以基本上,假设您定义了一个函数,然后在函数之后在()括号中写入一些东西.如果我写函数(同性恋),它最终会被'错误名称未定义'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(同性恋)时,无论我做什么,它都会以错误结束.有没有办法避免它?不使用""引号.

python string input function python-2.7

1
推荐指数
1
解决办法
347
查看次数

标签 统计

python ×2

python-2.7 ×2

cmd ×1

function ×1

input ×1

irb ×1

ruby ×1

string ×1