提交 241f1a1c 编写于 作者: F feilong

改进2题

上级 06630256
{
"one_line": {
"token[\"key\"] ": [
"token"
],
"print(\"\".join(result))": [
"print(result)"
],
"for token in tokens": [
"for t in tokens"
],
"result = []": [
"result = {}"
]
},
"source": "process.py",
"source": "process.md",
"depends": [],
"exercise_id": 27,
"type": "code_options"
......
# 编程语言简史
历史上有很多编程语言,他们在编程语言发展的过程中都起到过重要的作用。
下面的 Python 数组包含了历史上的大部分编程语言。
```python
languages = ["Regional Assembly Language","Autocode","FORTRAN","IPL (LISP的先驱)","FLOW-MATIC (COBOL的先驱)","COMTRAN (COBOL的先驱)","LISP","ALGOL 58","FACT (COBOL的先驱)","COBOL","APL","Simula","SNOBOL","CPL (C的先驱)","BASIC","PL/I","BCPL (C的先驱)","Logo","Pascal","Forth","C语言","Smalltalk","Prolog","ML","Scheme","SQL","Ada","C++","Common Lisp","MATLAB","Eiffel","Objective-C","Erlang","Perl","Tcl","FL (Backus)","Haskell","Python","Visual Basic","HTML","Ruby","Lua","CLOS (part of ANSI Common Lisp)","Java","Delphi (Object Pascal)","JavaScript","PHP","REBOL","D","C#","Visual Basic .NET","F#","Scala","Factor","Windows PowerShell","Rust","Clojure","Go"]
```
下面的 Python 数组包含了这些编程对应的创建时间。
```python
years = [1951, 1952, 1954, 1954, 1955, 1957, 1958, 1958, 1959, 1959, 1962, 1962, 1962, 1963, 1964, 1964, 1967 ,1968 ,1970 ,1970 ,1972 ,1972 ,1972 ,1973 ,1975 ,1978 ,1980 ,1983 ,1984 ,1984 ,1985 ,1986 ,1986 ,1987 ,1988 ,1989 ,1990 ,1991 ,1991 ,1991 ,1993 ,1993 ,1994 ,1995 ,1995 ,1995 ,1995 ,1997 ,1999 ,2001 ,2001 ,2002 ,2003 ,2003 ,2006 ,2006 ,2007 ,2009]
```
编写一个 Python 程序,每行打印每个编程语言的名字和对应的创建时间,例如:
```python
Regional Assembly Language : 1951
Autocode : 1952
FORTRAN : 1954
...
```
请找出以下代码中<span style="color:red">错误</span>的选项。
## template
```python
if __name__ == '__main__':
languages = ["Regional Assembly Language","Autocode","FORTRAN","IPL (LISP的先驱)","FLOW-MATIC (COBOL的先驱)","COMTRAN (COBOL的先驱)","LISP","ALGOL 58","FACT (COBOL的先驱)","COBOL","APL","Simula","SNOBOL","CPL (C的先驱)","BASIC","PL/I","BCPL (C的先驱)","Logo","Pascal","Forth","C语言","Smalltalk","Prolog","ML","Scheme","SQL","Ada","C++","Common Lisp","MATLAB","Eiffel","Objective-C","Erlang","Perl","Tcl","FL (Backus)","Haskell","Python","Visual Basic","HTML","Ruby","Lua","CLOS (part of ANSI Common Lisp)","Java","Delphi (Object Pascal)","JavaScript","PHP","REBOL","D","C#","Visual Basic .NET","F#","Scala","Factor","Windows PowerShell","Rust","Clojure","Go"]
years = [1951, 1952, 1954, 1954, 1955, 1957, 1958, 1958, 1959, 1959, 1962, 1962, 1962, 1963, 1964, 1964, 1967 ,1968 ,1970 ,1970 ,1972 ,1972 ,1972 ,1973 ,1975 ,1978 ,1980 ,1983 ,1984 ,1984 ,1985 ,1986 ,1986 ,1987 ,1988 ,1989 ,1990 ,1991 ,1991 ,1991 ,1993 ,1993 ,1994 ,1995 ,1995 ,1995 ,1995 ,1997 ,1999 ,2001 ,2001 ,2002 ,2003 ,2003 ,2006 ,2006 ,2007 ,2009]
for i in range(0, len(languages)):
language = languages[i]
year = years[i]
print(language, ':', year)
i = 0
while i < len(years):
language = languages[i]
year = years[i]
print(language, ':', year)
i += 1
[print(languages[i], ':', years[i]) for i in range(0, len(languages))]
```
## 答案
```python
if __name__ == '__main__':
languages = ...
years = ...
for language in languages:
for year in years:
print(language, ':', year)
```
## 选项
### for循环打印
```python
if __name__ == '__main__':
languages = ...
years = ...
for i in range(0, len(languages)):
language = languages[i]
year = years[i]
print(language, ':', year)
```
### while循环打印
```python
if __name__ == '__main__':
languages = ...
years = ...
i = 0
while i < len(years):
language = languages[i]
year = years[i]
print(language, ':', year)
i += 1
```
### 列表表达式打印
```python
if __name__ == '__main__':
languages = ...
years = ...
[print(languages[i], ':', years[i]) for i in range(0, len(languages))]
```
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:基本的 Python 处理
# 描述:Python 过程性代码,用循环合并“猴子在苹果树下打架”文本并打印
# 标题:编程语言简史
# 描述:循环打印每一个编程语言的创建年份
if __name__ == '__main__':
tokens = [
{
"key": "猴子在"
},
{
"key": "苹果树下"
},
{
"key": "打架"
}
]
result = []
for token in tokens:
result.append(token["key"])
print("".join(result))
languages = ["Regional Assembly Language", "Autocode", "FORTRAN", "IPL (LISP的先驱)", "FLOW-MATIC (COBOL的先驱)", "COMTRAN (COBOL的先驱)", "LISP", "ALGOL 58", "FACT (COBOL的先驱)", "COBOL", "APL", "Simula", "SNOBOL", "CPL (C的先驱)", "BASIC", "PL/I", "BCPL (C的先驱)", "Logo", "Pascal", "Forth", "C语言", "Smalltalk", "Prolog", "ML", "Scheme", "SQL", "Ada", "C++", "Common Lisp",
"MATLAB", "Eiffel", "Objective-C", "Erlang", "Perl", "Tcl", "FL (Backus)", "Haskell", "Python", "Visual Basic", "HTML", "Ruby", "Lua", "CLOS (part of ANSI Common Lisp)", "Java", "Delphi (Object Pascal)", "JavaScript", "PHP", "REBOL", "D", "C#", "Visual Basic .NET", "F#", "Scala", "Factor", "Windows PowerShell", "Rust", "Clojure", "Go"]
years = [1951, 1952, 1954, 1954, 1955, 1957, 1958, 1958, 1959, 1959, 1962, 1962, 1962, 1963, 1964, 1964, 1967, 1968, 1970, 1970, 1972, 1972, 1972, 1973, 1975, 1978, 1980, 1983,
1984, 1984, 1985, 1986, 1986, 1987, 1988, 1989, 1990, 1991, 1991, 1991, 1993, 1993, 1994, 1995, 1995, 1995, 1995, 1997, 1999, 2001, 2001, 2002, 2003, 2003, 2006, 2006, 2007, 2009]
for i in range(0, len(languages)):
language = languages[i]
year = years[i]
print(language, ':', year)
i = 0
while i < len(years):
language = languages[i]
year = years[i]
print(language, ':', year)
i += 1
[print(languages[i], ':', years[i]) for i in range(0, len(languages))]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册