基本上 // Create a Pattern object
Pattern pattern = Pattern.compile("PT(\\d+H)?(\\d+M)?(\\d+S)?");
// Now create matcher object.
Matcher matcher = pattern.matcher(duracaoStr);
int hour = 0;
int minute = 0;
int second = 0;
if(matcher.matches()){
for(int i = 1; i<=matcher.groupCount();i++){
String group = matcher.group(i);
int number = new Integer(group.substring(0, group.length()-1));
if(matcher.group(i).endsWith("H")){
hour = number;
} else if(matcher.group(i).endsWith("M")){
minute = number;
} else if(matcher.group(i).endsWith("S")){
second = number;
}
}
}
方法接受用户输入,检查并在用户未输入main
时调用first
方法。
quit
方法检查输入的第一部分,并根据用户输入的内容调用其他方法之一。这是我得到错误的一点;例如,当first
方法调用first
方法时,我会收到form
个异常。我对此有点困惑,因为我已经定义了每个方法,并且它们都拼写正确,当我调用NameError: name 'form' is not defined
方法时,它完全正常。
主要方法:
quit
第一种方法:
if __name__ == '__main__':
for line in sys.stdin:
s = line.strip()
if not s: break
if (str(s) == "quit"): quit()
elif (str(s) == "quit") == False:
a = s.split()
print(a)
if (len(a) is 2): first(a)
elif (len(a) is 3): first(a)
else: print("Invalid Input. Please Re-enter.")
其他方法(这些都是从第一种方法调用的):
def first(a = list()):
word = a[0]
if word == "ls":
ls(a[1])
elif word == "format":
form(a[1])
elif word == "reconnect":
reconnect(a[1])
elif word == "mkfile":
mkfile(a[1])
elif word == "mkdir":
mkdir(a[1])
elif word == "append":
append(a[1], a[2])
elif word == "delfile":
delfile(a[1])
elif word == "deldir":
deldir(a[1])
else:
print("Invalid Prompt. Please Re-enter.")
答案 0 :(得分:1)
这个错误是因为
elif word == "format":
form(a[1])
python基本上不知道是什么形式。
让我告诉你:
gaf@$[09:21:56]~> python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> form()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'form' is not defined
>>>
有两种方法
>>> def form():
... pass
...
>>> form()
>>> form
<function form at 0x7f49d7f38a28>
>>>
或使用
从一些库中导入它import
命令
也是订单也很重要
try:
form()
except NameError:
print('Oops name error raise above')
def form():
print('form foo is called')
try:
form()
except NameError:
print('Oops name error raise below')
会给你
/home/gaf/dashboard/bin/python /home/gaf/PycharmProjects/dashboard/test.py
Oops name error raise above
form foo is called
Process finished with exit code 0
P.S。 看看pep8 你的代码是一团糟%) 但不用担心每个人用第一语言做什么
答案 1 :(得分:0)
这取决于您是否使用python 2.7或3,但您的代码可以进行一些小的更改。
import sys
def reconnect(one=""):
print("Reconnect")
def ls(one=""):
print("list")
def mkfile(one=""):
print("make file")
def mkdir(one=""):
print("make drive")
def append(one="", two=""):
print("append")
def form(one=""):
print("format " + one)
def delfile(one=""):
print("delete file")
def deldir(one=""):
print("delete directory")
def quit():
print("quit")
sys.exit(0)
def first(a=list()):
word = a[0]
if word == "ls":
ls(a[1])
elif word == "format":
form(a[1])
elif word == "reconnect":
reconnect(a[1])
elif word == "mkfile":
mkfile(a[1])
elif word == "mkdir":
mkdir(a[1])
elif word == "append":
append(a[1], a[2])
elif word == "delfile":
delfile(a[1])
elif word == "deldir":
deldir(a[1])
else:
print("Invalid Prompt. Please Re-enter.")
line = raw_input("Some input please: ") # or `input("Some...` in python 3
print(line)
s = line.strip()
if (str(s) == "quit"):
quit()
elif (str(s) == "quit") == False:
a = s.split()
print(a)
if (len(a) is 2):
first(a)
elif (len(a) is 3):
first(a)
else:
print("Invalid Input. Please Re-enter.")
测试
python pyprog.py
Some input please: ls text.txt
ls text.txt
['ls', 'text.txt']
list
您也可以尝试online。