使用 input() 函数在 Octave 中获取键盘输入

发布于 2024-11-09 06:32:20 字数 1364 浏览 1 评论 0原文

我目前通过 Xoctave 在 Windows 中使用 Octave,但遇到以下问题。

我尝试运行一个以

activity = input("What would you like to do? ");

When I call this script in the console, it prints out

What would you like to do?

When I call this script in the console, it prints out When I key in a number and press Enter, it returns:

What would you like to do? 1

error: `What' undefined near line 4 column 1

>> 

When I key in a number and press Enter, it returns:开头的脚本 (即尝试解析整行,包括提示文本)。

为了更清楚地了解我想说的内容,只需保存一个仅包含以下行的脚本:

m = input('Enter m: ');

然后尝试在 Xoctave 中运行该脚本。我相信你也会遇到同样的错误。

我相信这与更改默认解析行为有关,但我不确定如何更改。

@Bart_Kiers
我认为你误解了=\ 当我按照您刚刚规定的操作时,它会存储: 活动=你想做什么? 1 我希望它只存储 Activity=1 我发现做到这一点的唯一方法是: Activity1=input("选择 1 或 0"); Activity2=strrep(activity,"选择 1 或 0",""); Activity=str2num(activity2)

它将输出到: Activity=1

为了澄清这个问题,它使用菜单功能创建了一个类似但更令人沮丧的问题。 如果我这样做:

number=menu("What's your favorite number?",1,2,3,4,5); 

它会提示:

[ 1] 1
[ 2] 2
[ 3] 3
[ 4] 4
[ 5] 5

pick a number, any number:  

我键入 3 并按 Enter 键,它会返回:“错误:输入无效或超出范围”,因为它正在尝试解析整行,包括提示! =[救命!

另外,我使用 Xoctave 与 Windows 中的 Octave 命令行进行交互。 不确定这是否有帮助。

谢谢!

I'm currently using Octave in Windows through Xoctave and I'm having the following problem.

I attempted to run a script that starts with

activity = input("What would you like to do? ");

When I called this script in the console, it prints out

What would you like to do?

When I key in a number and press enter, it returns:

What would you like to do? 1

error: `What' undefined near line 4 column 1

>> 

It seems as if the console is trying to parse the output of the previous command (i.e., trying to parse the whole line, including the prompt text).

To get a clearer picture of what I'm trying to say, just save a script with only the line:

m = input('Enter m: ');

then try to run that script in Xoctave. I believe you'll get the same error too.

I believe this has to do with changing the default parsing behavior, but I'm not sure how.

@Bart_Kiers
I think you misunderstand =\
When I do what you just prescribed, it stores:
activity = What would you like to do? 1
I want it to just store activity=1
the only way I've found to do this is to do:
activity1=input("Pick 1 or 0");
activity2=strrep(activity,"Pick 1 or 0 ","");
activity=str2num(activity2)

To which it will output:
activity=1

To clarify the issue, it creates a similar, yet significantly more frustrating problem using the menu function.
if i do this:

number=menu("What's your favorite number?",1,2,3,4,5); 

it prompts:

[ 1] 1
[ 2] 2
[ 3] 3
[ 4] 4
[ 5] 5

pick a number, any number:  

I key in 3 and press enter and it returns: "error: input invalid or out of range" because it is trying to parse the entire line, including the prompt! =[ help!

Also, I'm using Xoctave to interface with the Octave command line in Windows.
Not sure if this helps.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-11-16 06:32:20

我无法复制您的错误,但您可以尝试:

m = input("Enter m: \n");

新行不适用于菜单。

I can't replicate your error, but you might try:

m = input("Enter m: \n");

New line won't work with menu though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文