我在codecademy上学习python。到目前为止一切顺利,但我无法理解为什么我的代码以一种方式工作而不是一种不同的方式。所以这是我所做的代码:
$ git checkout <old branch>
$ git pull
n = ["Michael", "Lieberman"]
def join_strings(words):
result = ""
for i in range(len(words)):
result += i
return result
print join_strings(n)
我的问题更多的是关于“单词[i]”的混淆,以及是什么使得该代码与我的方式相比起作用?感谢
答案 0 :(得分:0)
i
将是一个整数,而不是word[]
中的单词,因为这是range()
返回的内容。 len(words)
还返回一个整数,即length
或单词数组的计数。
您可以通过从word[]
开始将数组的索引传递到0
来访问该单词,例如word[0]
将是数组的第一个元素,word[1]
将是第二个。
有关range()的详细信息: http://pythoncentral.io/pythons-range-function-explained/
有关len()的详细信息: https://www.tutorialspoint.com/python/list_len.htm
答案 1 :(得分:0)
在第一个中,你将循环的整数加起来
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<title>Year 7 English Quiz - Are you smarter than an 11 year old?</title>
</head>
<body>
<div class="container">
<h1>Year 7 English Quiz - Are you smarter than an 11 year old?</h1><hr>
<h3>Rules: (Read before playing)</h3>
<p>'-' Means fill in the blank with a word</p>
<p>'|' Means fill in the blank with a phrase</p>
<p>'+' Means complete the word</p>
<p>There are 33 questions in this quiz.</p>
<p>By hitting the timer to the bottom, you can challenge yourself and see how quick you could complete the quiz.</p>
<p>11 will be a set of easy questions, another 11 will be a set of medium questions and the last 11 will be hard / or virtually impossible for a Year 7 to figure out.</p>
<p>These difficulty levels only apply to Year 7s. A hard question could be simple to you.</p>
<p>Have fun and don't get mad if you don't pass the test. If you fail, then that doesn't necessarily bad at English!</p>
<p>Don't click a button two times during a quiz unless you want to mess everything up. But I know you're probably a rebel.</p>
<p>Scroll down after each question, as some may not show on your screen and will be below</p>
<p>At the end, your points will be tallied up. Through hard, you will receive negative points for answering a question incorrectly.</p>
<button class="btn btn-default" onclick="stopwatch">Stopwatch</button>
<span data-toggle="collapse" data-target="#q1" id="disableoc"><button type="button" class="btn btn-default"><a href="#" data-toggle="tooltip" data-placement="bottom" title="Chill Out M8">Just get to the questions already!</a></button></span><hr>
<div id="q1" class="collapse">
<h1>Easy Questions:</h1><br>
<div class="jumbotron"><h1 class="question">Question 1: When is _ birthday?</h1></div>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q2">Answer 1: Your</button>
<button onclick="minus1()" class="btn btn-danger" data-toggle="collapse" data-target="#q2">Answer 2: You're</button><hr>
</div>
<div id="q2" class="collapse">
<div class="jumbotron"><h1 class="question">Question 2: +ranky</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q3">Answer 1: Kranky</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q3">Answer 2: Cranky</button><hr>
</div>
<div id="q3" class="collapse">
<div class="jumbotron"><h1 class="question">Question 3: Spot the word class that doesn't exist.</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q4">Answer 1: Determiner</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q4">Answer 2: Verb</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q4">Answer 3: Noun</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q4">Answer 4: Preposition</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q4">Answer 5: Proverb</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q4">Answer 6: Adjective</button><hr>
</div>
<div id="q4" class="collapse">
<div class="jumbotron"><h1 class="question">Question 4: It's - cats and dogs.</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q5">Answer 1: Pouring</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q5">Answer 2: Raining</button><hr>
</div>
<div id="q5" class="collapse">
<div class="jumbotron"><h1 class="question">Question 5: +yclone</h1></div>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q6">Answer 1: Cyclone</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q6">Answer 2: Syclone</button><hr>
</div>
<div id="q6" class="collapse">
<div class="jumbotron"><h1 class="question">Question 6: I - cheeseburger.</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q7">Answer 1: Haz</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q7">Answer 2: Have</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q7">Answer 3: Has a</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q7">Answer 4: Have a</button><hr>
</div>
<div id="q7" class="collapse">
<div class="jumbotron"><h1 class="question">Question 7: Find the tense of the following sentence: I located my nearest barber.</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q8">Answer 1: Present</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q8">Answer 2: Simple</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q8">Answer 3: Continuous</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q8">Answer 4: Past</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q8">Answer 5: Future</button><hr>
</div>
<div id="q8" class="collapse">
<div class="jumbotron"><h1 class="question">Question 8: - the remote control.</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q9">Answer 1: Gimme</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q9">Answer 2: Give me</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q9">Answer 3: Gave me</button><hr>
</div>
<div id="q9" class="collapse">
<div class="jumbotron"><h1 class="question">Question 9: What is the closest definition to a synonym?</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q10">Answer 1: A word used to describe a verb</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q10">Answer 2: A group of words that acts in the same way as a participle</button><br>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q11">Answer 3: A word that is similar if not identical to another word</button><hr>
</div>
<div id="q10" class="collapse">
<div class="jumbotron"><h1 class="question">Question 10: What is the closest definition to a noun phrase?</h1></div>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#q11">Answer 1: Answer 2 but the noun *must* be either a subject or object</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q11">Answer 2: A phrase with 1 or more noun(s) in it</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#q11">Answer 3: A noun that also acts a phrase</button><hr>
</div>
<div id="q11" class="collapse">
<div class="jumbotron"><h1 class="question">Question 11: The he was entirely - in frozen carbonite.</h1></div>
<button class="btn btn-danger" data-toggle="collapse" data-target="#eqsummary" onclick="eqs()">Answer 1: Encasted</button>
<button onclick="add1()" class="btn btn-danger" data-toggle="collapse" data-target="#eqsummary" onclick="eqs()">Answer 2: Encapsulated</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#eqsummary" onclick="eqs()">Answer 3: Capsulated</button>
<button class="btn btn-danger" data-toggle="collapse" data-target="#eqsummary" onclick="eqs()">Answer 4: Capered</button><hr>
</div>
<div id="eqsummary">
<div class="jumbotron"><p id="sum1"></p></div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
现在,如果你写:
n = ["Michael", "Lieberman"]
for i in range(len(n)):
result += i # i = 0 then i = 1, sum up to 1
print i # add this to see what you are adding up to your list
return result
它会返回列表n[0]
中的第一个元素,然后是代码:
n = ["Michael", "Lieberman"]
将返回列表中的内容的串联,而不是在第一个代码中添加循环的整数。