Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 TypeError:getch()只接受0个参数(给定1个)_Python 2.7 - Fatal编程技术网

Python 2.7 TypeError:getch()只接受0个参数(给定1个)

Python 2.7 TypeError:getch()只接受0个参数(给定1个),python-2.7,Python 2.7,我有这段代码 如果输入的不是1,2,3,4,我想再次输入 import msvcrt answer = msvcrt.getch() while answer not in ['1','2','3','4']: answer = msvcrt.getch('Enter a valid option (1,2,3,4): ') 输入列表中未包含的任何内容会出现以下错误: TypeError: getch() takes exactly 0 arguments (1 given) 但是

我有这段代码 如果输入的不是1,2,3,4,我想再次输入

import msvcrt
answer = msvcrt.getch()
while answer not in ['1','2','3','4']:
    answer = msvcrt.getch('Enter a valid option (1,2,3,4):  ')
输入列表中未包含的任何内容会出现以下错误:

TypeError: getch() takes exactly 0 arguments (1 given)
但是我不知道为什么它会给我这个错误
感谢您提供的任何帮助

此行认为您正在尝试向
getch
传递一个不允许的值

answer = msvcrt.getch('Enter a valid option (1,2,3,4):  ')
文件:


虽然第一次使用getch没有问题,但第二次使用会引发错误。

错误消息的意思正是它所说的:
msvcrt.getch()是一个不带参数的函数

您在这里用一个参数调用它:

answer = msvcrt.getch('Enter a valid option (1,2,3,4):  ')

如果要打印提示,请先使用单独的
print
调用。

错误文本是不言自明的。在第二行中,您正确地调用了getch:

answer = msvcrt.getch()
为了让代码执行预期的操作,请将其更改为:

while answer not in ['1','2','3','4']:
    print 'Enter a valid option (1,2,3,4):  '
    answer = msvcrt.getch()