【发布时间】:2016-09-03 17:06:29
【问题描述】:
我只是需要一些帮助来解决这个问题。所有细节都在下面提到的问题中。我已经做了一个解决方案,但我有点坚持这一点,如果有人能引导我走向正确的方向,我将不胜感激。
问题:每当我运行我的程序时,它总是说“你有:三个 5”,即使重复三次的数字不是 5。希望这是有道理的。我的程序在问题下方。
上面的网格可以在 Python 中表示为一个列表变量,如grid = [2,2,1,3,2]
编写一个 Python 函数 pokieTester(list),它将接收一个列表作为参数并
计算列表中是否有 3,4 或 5 个相同的数字
它将返回一个包含 2 个项目的列表:
o The first item will be the number of repeated numbers
o The second item will be the value of repeated numbers
编写一个没有参数的 Python 函数 makeList(),返回一个包含 5 个随机整数(包括 1 到 5)的列表
将您的函数组合到一个程序中,该程序显示适当的文本,说明用户是赢还是输。
(如果你没有 3、4 或 5 个相同的数字,你就输了。)
例如: 你赢了!或者对不起你输了
如果他们赢了,它应该显示有多少重复的数字和它们的值。
例如:
你赢了!你有三个 2
====== 这是我的程序=====
import random
def pokieTester(countList):
count = countList.count(0)
for num in [1,2,3,4,5]:
if count < 3:
count = countList.count(num)
demoList = [count, num]
return demoList
def makeList():
numList = []
for count in [0,1,2,3,4]:
a = random.randint(1,5)
numList.append(a)
return numList
gameList = makeList()
print (gameList)
matches = pokieTester(gameList)
count = matches[0]
num = matches[1]
num_List = ['three','four','five']
if count > 2:
print ("You win")
print ("You have: " , end = '' )
if count == 3:
print (num_List[0], num, end = '')
elif count == 4:
print (num_List[1], num, end ='')
elif count == 5:
print (num_List[2], num, end ='')
else:
print ("Sorry, You lose")
【问题讨论】:
标签: python list python-3.5