Python 比较列表中的for或while循环
我们想写一个函数,当一个数字列表 可以解释为“Yahtzee”列表中的所有五个数字都必须 都一样。编写一个名为isYahtzee(aList)的函数, 作为参数,一个包含5个数字的列表,并返回一个Python 比较列表中的for或while循环,python,for-loop,while-loop,Python,For Loop,While Loop,我们想写一个函数,当一个数字列表 可以解释为“Yahtzee”列表中的所有五个数字都必须 都一样。编写一个名为isYahtzee(aList)的函数, 作为参数,一个包含5个数字的列表,并返回一个布尔值。如果五个人 数字是相同的,它应该返回True,否则,它应该返回 返回False。例如,isYahtzee([1,2,3,4,5])应该返回 False和isYahtzee([1,1,1,1,1])应返回True。你必须使用 检查时,此函数中的“for loop”或“while loop” 列表中
布尔值。如果五个人
数字是相同的,它应该返回True
,否则,它应该返回
返回False
。例如,isYahtzee([1,2,3,4,5])
应该返回
False
和isYahtzee([1,1,1,1,1])
应返回True
。你必须使用
检查时,此函数中的“for loop”
或“while loop”
列表中的值
这就是我到目前为止所做的,我一直在犯错误
def isYahtzee(aList):
for i in Range(0,5):
if i != i+1:
return false
else:
return true
isYahtzee(1,2,3,4,5)
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
builtins.TypeError: isYahtzee() takes 1 positional argument but 5 were given
def isYahtzee(列表):
对于范围(0,5)内的i:
如果我i+1:
返回错误
其他:
返回真值
isYahtzee(1,2,3,4,5)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
builtins.TypeError:isYahtzee()接受1个位置参数,但给出了5个
我认为这样更好:
def isYahtzee(aList):
return len(set(aList)) == 1
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
我认为这样更好:
def isYahtzee(aList):
return len(set(aList)) == 1
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
我认为这样更好:
def isYahtzee(aList):
return len(set(aList)) == 1
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
我认为这样更好:
def isYahtzee(aList):
return len(set(aList)) == 1
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
这是一个使用的版本,而的版本更容易实现
def isYahtzee(aList):
i = 0
while i < len(aList) - 1:
if aList[i] != aList[i + 1]:
return False
i += 1
return True
这是一个使用的版本,而的版本更容易实现
def isYahtzee(aList):
i = 0
while i < len(aList) - 1:
if aList[i] != aList[i + 1]:
return False
i += 1
return True
这是一个使用的版本,而的版本更容易实现
def isYahtzee(aList):
i = 0
while i < len(aList) - 1:
if aList[i] != aList[i + 1]:
return False
i += 1
return True
这是一个使用的版本,而的版本更容易实现
def isYahtzee(aList):
i = 0
while i < len(aList) - 1:
if aList[i] != aList[i + 1]:
return False
i += 1
return True
您的代码存在许多问题:
isYahtzee
需要一个单参数(列表),但您要传入五个。这就是引起类型错误的原因
范围
应为小写
true
和false
应大写
i
永远不会等于i+1
。因此,if语句的条件将始终计算为True
您的函数从不检查甚至使用aList
中的项目
下面是一个版本的isYahtzee
,它可以工作:
# Declare the function isYahtzee
def isYahtzee(aList):
# Iterate over the items in aList
for item in aList:
# See if the current item is different from the first
if item != aList[0]:
# If so, return False because not all items in aList are duplicates
return False
# If we get here, return True because all items in aList are duplicates
return True
下面是一个演示:
>>> def isYahtzee(aList):
... for item in aList:
... if item != aList[0]:
... return False
... return True
...
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
>>>
您的代码存在许多问题:
isYahtzee
需要一个单参数(列表),但您要传入五个。这就是引起类型错误的原因
范围
应为小写
true
和false
应大写
i
永远不会等于i+1
。因此,if语句的条件将始终计算为True
您的函数从不检查甚至使用aList
中的项目
下面是一个版本的isYahtzee
,它可以工作:
# Declare the function isYahtzee
def isYahtzee(aList):
# Iterate over the items in aList
for item in aList:
# See if the current item is different from the first
if item != aList[0]:
# If so, return False because not all items in aList are duplicates
return False
# If we get here, return True because all items in aList are duplicates
return True
下面是一个演示:
>>> def isYahtzee(aList):
... for item in aList:
... if item != aList[0]:
... return False
... return True
...
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
>>>
您的代码存在许多问题:
isYahtzee
需要一个单参数(列表),但您要传入五个。这就是引起类型错误的原因
范围
应为小写
true
和false
应大写
i
永远不会等于i+1
。因此,if语句的条件将始终计算为True
您的函数从不检查甚至使用aList
中的项目
下面是一个版本的isYahtzee
,它可以工作:
# Declare the function isYahtzee
def isYahtzee(aList):
# Iterate over the items in aList
for item in aList:
# See if the current item is different from the first
if item != aList[0]:
# If so, return False because not all items in aList are duplicates
return False
# If we get here, return True because all items in aList are duplicates
return True
下面是一个演示:
>>> def isYahtzee(aList):
... for item in aList:
... if item != aList[0]:
... return False
... return True
...
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
>>>
您的代码存在许多问题:
isYahtzee
需要一个单参数(列表),但您要传入五个。这就是引起类型错误的原因
范围
应为小写
true
和false
应大写
i
永远不会等于i+1
。因此,if语句的条件将始终计算为True
您的函数从不检查甚至使用aList
中的项目
下面是一个版本的isYahtzee
,它可以工作:
# Declare the function isYahtzee
def isYahtzee(aList):
# Iterate over the items in aList
for item in aList:
# See if the current item is different from the first
if item != aList[0]:
# If so, return False because not all items in aList are duplicates
return False
# If we get here, return True because all items in aList are duplicates
return True
下面是一个演示:
>>> def isYahtzee(aList):
... for item in aList:
... if item != aList[0]:
... return False
... return True
...
>>> isYahtzee([1,1,1,1,1])
True
>>> isYahtzee([1,2,3,4,5])
False
>>>
在shot程序中有这么多错误:)在shot程序中有这么多错误:)在shot程序中有这么多错误:)在shot程序中有这么多错误:)OP说他被要求在
或
中使用一个,而
听起来像是一个家庭作业。我认为我们需要让CS prof思考得更有创造性。实际上,5项列表非常小,因此此解决方案比普通for循环要慢。集合只有在处理较大的列表时才非常有效。OP说他需要使用for
或while
——听起来像是家庭作业。我认为我们需要让CS Prof思考得更有创造性。实际上,5项列表太小了,这个解决方案会比正常的for循环慢。集合只有在处理较大的列表时才非常有效。OP说他需要使用for
或while
——听起来像是家庭作业。我认为我们需要让CS Prof思考得更有创造性。实际上,5项列表太小了,这个解决方案会比正常的for循环慢。集合只有在处理较大的列表时才非常有效。OP说他需要使用for
或while
——听起来像是家庭作业。我认为我们需要让CS Prof思考得更有创造性。实际上,5项列表太小了,这个解决方案会比正常的for循环慢。集合只有在处理较大的列表时才非常有效。