# 请问这个要怎么弄，没看懂要求

467 views

### 请问这个要怎么弄，没看懂要求

``````# DO *NOT* WRITE YOUR NAME TO MAINTAIN ANONYMITY FOR PLAGIARISM DETECTION
# Prompts the user for an arity (a natural number) n and a word.
# Call symbol a word consisting of nothing but alphabetic characters
# and underscores.
# Checks that the word is valid, in that it satisfies the following
# inductive definition:
# - a symbol, with spaces allowed at both ends, is a valid word;
# - a word of the form s(w_1,...,w_n) with s denoting a symbol and
#   w_1, ..., w_n denoting valid words, with spaces allowed at both ends and
#   around parentheses and commas, is a valid word.
import sys
def is_valid(word, arity):
return False
# REPLACE THE RETURN STATEMENT ABOVE WITH YOUR CODE
try:
arity = int(input('Input an arity : '))
if arity < 0:
raise ValueError
except ValueError:
print('Incorrect arity, giving up...')
sys.exit()
word = input('Input a word: ')
if is_valid(word, arity):
print('The word is valid.')
else:
print('The word is invalid.')``````

``````无效的：
\$ python3 quiz_3.py
Input an arity : 0
Input a word: f_1
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 0
Input a word: ()
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 0
Input a word: function_of_arity_one(hello)
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 1
Input a word: f)
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 1
Input a word: f[a]
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 2
Input a word: f(a, g(b))
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 3
Input a word: constant
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 3
Input a word: f((a,b,c))
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 3
Input a word: f(g(a,a), f(a,b))
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 3
Input a word: f(g(a,b,c),g(a,b,c),g(a,b,c)
The word is invalid.
\$ python3 quiz_3.py
Input an arity : 3
Input a word: f(a, g(a, b, f(a,b,c)), b, c)
The word is invalid.

Date: Term 3, 2020.
2 COMP9021 PRINCIPLES OF PROGRAMMING
\$ python3 quiz_3.py
Input an arity : 0
Input a word: a
The word is valid.
\$ python3 quiz_3.py
Input an arity : 1
Input a word: function_of_arity_one(hello)
The word is valid.
\$ python3 quiz_3.py
Input an arity : 2
Input a word: F(g(a,a), f(a,b))
The word is valid.
\$ python3 quiz_3.py
Input an arity : 3
Input a word: ff(ff(ff(a,b,ff(aa,bb,cc)) , b , ff(a,b,c)) , b , ff(a,ff(a,b,c),c))
The word is valid.
\$ python3 quiz_3.py
Input an arity : 4
Input a word: f(a, FF(a, b, fff(a, b, c, FfFf(a,b,c,d)), FfFf(a,b,c,d)), c,d)
The word is valid`````` by (71.8m points)
Prompts the user for an arity (a natural number) n and a word.

Call symbol a word consisting of nothing but alphabetic characters
and underscores.

Checks that the word is valid, in that it satisfies the following
inductive definition:
- a symbol, with spaces allowed at both ends, is a valid word;
- a word of the form s(w_1,...,w_n) with s denoting a symbol and
w_1, ..., w_n denoting valid words, with spaces allowed at both ends and
around parentheses and commas, is a valid word.

• 0 层：`word`
• 1 层：`word(word, word, word, ...)`，括号内的 `word` 两边都可以有空格
• 2 层：`word(word(word, word, ...), word(word, ...), ...)`