【发布时间】:2015-10-05 10:05:35
【问题描述】:
我知道 stdin.read_line() 函数,但我想通过使用或更符合 python 中的 raw_input() 的方法来减少代码的冗长。
所以我在this 关于 vala 的讨论中发现了 GNU ReadLine,但是我无法在 Genie 中重现它。
我要模仿的python代码是:
loop = 1
while loop == 1:
response = raw_input("Enter something or 'quit' to end => ")
if response == 'quit':
print 'quitting'
loop = 0
else:
print 'You typed %s' % response
我能做到的事情是:
[indent=4]
init
var loop = 1
while loop == 1
// print "Enter something or 'quit' to end => "
var response = ReadLine.read_line("Enter something or 'quit' to end => ")
if response == "quit"
print "quitting"
loop = 0
else
print "You typed %s", response
并尝试编译:
valac --pkg readline -X -lreadline loopwenquiry.gs
但我得到了错误:
loopwenquiry.gs:7.24-7.31: error: The name `ReadLine' does not exist in the context of `main'
var response = ReadLine.read_line("Enter something or 'quit' to end => ")
^^^^^^^^
loopwenquiry.gs:7.22-7.81: error: var declaration not allowed with non-typed initializer
var response = ReadLine.read_line("Enter something or 'quit' to end => ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
loopwenquiry.gs:8.12-8.19: error: The name `response' does not exist in the context of `main'
if response == "quit"
^^^^^^^^
loopwenquiry.gs:12.35-12.42: error: The name `response' does not exist in the context of `main'
print "You typed %s", response
^^^^^^^^
Compilation failed: 4 error(s), 0 warning(s)
我做错了什么?
谢谢。
【问题讨论】:
-
这是
Readline而不是ReadLine(注意大小写的区别)。 -
顺便说一句:你为什么不写你自己的
raw_input函数?