以下代码是我模拟彩票的尝试。
import random
def lottery(numbers):
lottoNumbers = [randint('0,100') for count in range(3)]
if numbers == lottoNumbers:
print('YOU WIN $10,000')
else:
print('YOU LOSE,DUN DUN DUNNN!')
return numbers
def main():
numbers = int(input('Enter a number: '))
if numbers == lottoNumbers:
numbers = lottery(numbers)
else:
numbers = lottery(numbers)
main()
嘿伙计们,在你给我的帮助下,我已经走到了尽头。我试图编写代码,以便随机选择3个乐透号码。然后用户必须输入他/她自己的乐透号码中的3个。如果他们得到所有3个正确,那么他们赢得整个奖项,如果他们得到3个数字但没有按照正确的顺序他们赢得一些奖品。显然,如果他们猜错了那么一份印刷声明会说明这一点。我感到困惑的是如何编写代码,以便用户可以输入3个数字来尝试匹配随机抽奖号码。我还想在用户输入他/她的选择后打印3个彩票号码。有什么想法吗?
感谢大家的帮助。
答案 0 :(得分:2)
你似乎对函数中参数的作用有点困惑。你已经说过你的randm函数接受参数“number”,但是你实际上并没有在任何地方使用它。下一次显示数字时,您已为其分配了一个全新的值,因此传递给randm的任何值实际上都没有被使用。
此外,当函数内未分配x时,该函数正在尝试返回x。你已经有一个名为x的全局变量已经被定义,在这种情况下,函数将只返回该变量,或者函数将失败,因为它找不到变量x。
这是我做过的一个快速示例,您将三个数字作为列表作为函数的参数传递。
import random
theirNumbers=[5,24,67]
def checkNumbers(theirNumbers):
lottoNumbers = []
for count in range(3)
lottoNumbers.append(random.randint(0,100))
winning = True
for number in theirNumbers:
if not each in lottoNumbers: winning=False
if winning == True: print("Winner!")
答案 1 :(得分:2)
您的实施存在一些问题,仅举几例:
如果您尝试将函数randm
的输出与x进行比较,则需要在函数中包含返回值,如下所示:
def randm():
return return_value
您似乎正在打印所有值但未存储它们,最后您将只得到最后一个,您应该尝试将它们存储在如下列表中:
list_name = [randint(0,100) for x in range(x)]
这会在列表中生成randint(0,100)
x
次,这样您以后就可以访问所有值。
要尽可能接近您尝试的代码修复代码,我会这样做:
import random
def randm(user_numbers):
number = []
for count in range(3):
number.append(random.randint(0, 100))
print(number)
return user_numbers == number
if randm(x):
print('WINNER')
如果您正在寻找一种非常pythonic的方式来完成这项任务, 你可能想尝试这样的事情:
from random import randint
def doLotto(numbers):
# make the lotto number list
lottoNumbers = [randint(0,100) for x in range(len(numbers))]
# check to see if the numbers were equal to the lotto numbers
if numbers == lottoNumbers:
print("You are WinRar!")
else:
print("You Lose!")
我假设您正在使用python 3.x +
从您的代码(具体是print())答案 2 :(得分:1)
尝试发布您的整个代码。还要注意发布时的缩进,看起来你的函数定义是空的。
我会这样做:
import random
def lottery():
win = True
for i in range(3):
guess = random.randint(1,100)
if int(raw_input("Please enter a number...")) != guess:
win = False
break
return win
答案 3 :(得分:1)
这样做只需几步即可完成。 在编写代码时你应该学习的第一件事是让单独的代码片段(函数或对象)完成不同的工作。
首先让创建功能来进行抽奖:
def makeLottery(slotCount, maxNumber):
return tuple(random.randint(1,maxNumber) for slot in range(slotCount))
接下来让create function询问用户的猜测:
def askGuess(slotCount, maxNumber):
print("take a guess, write {count} numbers separated by space from 1 to {max}".format(count = self.slotCount, max = self.maxNumber))
while True: #we will ask user until he enter sumething suitable
userInput = raw_input()
try:
numbers = parseGuess(userInput,slotCount,maxNumber)
except ValueError as err:
print("please ensure your are entering integer decimal numbers separated by space")
except GuessError as err:
if err.wrongCount: print("please enter exactly {count} numbers".format(count = slotCount))
if err.notInRange: print("all number must be in range from 1 to {max}".format(max = maxNumber))
return numbers
这里我们使用另一个函数和自定义异常类,让我们创建它们:
def parseGuess(userInput, slotCount,maxNumber):
numbers = tuple(map(int,userInput.split()))
if len(numbers) != slotCount : raise GuessError(wrongCount = True)
for number in numbers:
if not 1 <= number <= maxNumber : raise GuessError(notInRange = True)
return numbers
class GuessError(Exception):
def __init__(self,wrongCount = False, notInRange = False):
super(GuessError,self).__init__()
self.wrongCount = wrongCount
self.notInRange = notInRange
最后用于检查解决方案,如果他将赢得比赛,请与用户保持联系:
def checkGuess(lottery,userGuess):
if lottery == userGuess : print "BINGO!!!!"
else : print "Sorry, you lost"
正如您所看到的,这里有许多功能使用通用数据。所以它应该建议你在单个类中收集整个代码,让我们这样做:
class Lottery(object):
def __init__(self, slotCount, maxNumber):
self.slotCount = slotCount
self.maxNumber = maxNumber
self.lottery = tuple(random.randint(1,maxNumber) for slot in range(slotCount))
def askGuess(self):
print("take a guess, write {count} numbers separated by space from 1 to {max}".format(count = self.slotCount, max = self.maxNumber))
while True: #we will ask user until he enter sumething suitable
userInput = raw_input()
try:
numbers = self.parseGuess(userInput)
except ValueError as err:
print("please ensure your are entering integer decimal numbers separated by space")
continue
except GuessError as err:
if err.wrongCount: print("please enter exactly {count} numbers".format(count = self.slotCount))
if err.notInRange: print("all number must be in range from 1 to {max}".format(max = self.maxNumber))
continue
return numbers
def parseGuess(self,userInput):
numbers = tuple(map(int,userInput.split()))
if len(numbers) != self.slotCount : raise GuessError(wrongCount = True)
for number in numbers:
if not 1 <= number <= self.maxNumber : raise GuessError(notInRange = True)
return numbers
def askAndCheck(self):
userGuess = self.askGuess()
if self.lottery == userGuess : print "BINGO!!!!"
else : print "Sorry, you lost"
最后让我们检查它的工作原理:
>>> lottery = Lottery(3,100)
>>> lottery.askAndCheck()
take a guess, write 3 numbers separated by space from 1 to 100
3
please enter exactly 3 numbers
1 10 1000
all number must be in range from 1 to 100
1 .123 asd
please ensure your are entering integer decimal numbers separated by space
1 2 3
Sorry, you lost
>>> lottery = Lottery(5,1)
>>> lottery.askAndCheck()
take a guess, write 5 numbers separated by space from 1 to 1
1 1 1 1 1
BINGO!!!!