Python彩票程序中的逻辑错误
我正在编写一个代码,用户在其中输入6个介于1和59之间的“彩票”号码-该位工作正常。 然后,计算机从1到59之间随机选取6个数字,该位也能正常工作。 然而,程序必须比较两个数组中的哪些数字匹配,并且必须输出“youhave(number)matches”,此时它匹配一些数字,但在某些地方存在逻辑错误 为了弄清楚发生了什么,我试着改变程序,让计算机选择6个介于1和6之间的数字,这样它得到的每个数组都有完全相同的数字。我把用户的号码输入为1到6,所以从技术上讲,程序应该输出“Youhave6matches”,但相反,它输出“Youhave4matches”,其中有些是3个,有些是2个Python彩票程序中的逻辑错误,python,arrays,python-2.7,debugging,string-matching,Python,Arrays,Python 2.7,Debugging,String Matching,我正在编写一个代码,用户在其中输入6个介于1和59之间的“彩票”号码-该位工作正常。 然后,计算机从1到59之间随机选取6个数字,该位也能正常工作。 然而,程序必须比较两个数组中的哪些数字匹配,并且必须输出“youhave(number)matches”,此时它匹配一些数字,但在某些地方存在逻辑错误 为了弄清楚发生了什么,我试着改变程序,让计算机选择6个介于1和6之间的数字,这样它得到的每个数组都有完全相同的数字。我把用户的号码输入为1到6,所以从技术上讲,程序应该输出“Youhave6matc
import random
lotterynumbers = []
usernumbers = []
compnum = 0
usernum = 0
matches = 0
#user picks numbers
while len(usernumbers) < 6:
try:
usernum = int(input("input a number between 1 and 59 "))
except ValueError: #if the input is not an integer
print("your input must be a number between 1 and 59 ")
continue
if usernum > 59 or usernum < 1:
print("your number has to be between 1 and 59 ")
continue
elif usernum in usernumbers: #if the input has already been entered into the array
print("you have already used this number")
continue
else:
usernumbers.append(usernum)
timesplay = int(input("how many times would you like to play the lottery? "))
timesplayed = 0
while timesplayed < timesplay:
counter = 0
while len(lotterynumbers) < 6: #means the while loop runs whilst the array has less than 6 values
compnum = random.randint(1,59) #imports a random number between 1 and 59
while compnum in lotterynumbers:
compnum = random.randint(1,59)
lotterynumbers.append(compnum) #adds numbers to the array
if usernumbers[counter] in lotterynumbers:
matches = matches + 1 #check computer numbers against the user numbers
counter = counter + 1
print (lotterynumbers)
print ("you have ",matches,"matches")
lotterynumbers = [] #re-sets the array so the computer can find a new list of numbers
matches = 0 #re-sets matches to 0 for each draw of the lottery
timesplayed = timesplayed + 1
随机导入
乐透号码=[]
usernumbers=[]
compnum=0
usernum=0
匹配项=0
#用户选择数字
而len(用户编号)<6:
尝试:
usernum=int(输入(“输入一个介于1和59之间的数字”))
ValueError除外:#如果输入不是整数
打印(“您的输入必须是介于1和59之间的数字”)
持续
如果usernum>59或usernum<1:
打印(“您的号码必须介于1和59之间”)
持续
usernumbers中的elif usernum:#如果已将输入输入到数组中
打印(“您已使用此号码”)
继续
其他:
usernumbers.append(usernum)
timesplay=int(输入(“您想玩彩票多少次?”)
播放时间=0
时间播放<时间播放:
计数器=0
while len(lotterynumbers)<6:#表示当数组的值小于6时运行while循环
compnum=random.randint(1,59)#导入一个介于1和59之间的随机数
而乐透号码中的compnum:
compnum=random.randint(1,59)
lotterynumbers.append(compnum)#将数字添加到数组中
如果LotteryNumber中的UserNumber[计数器]:
匹配=匹配+1#对照用户号码检查计算机号码
计数器=计数器+1
打印(彩票号码)
打印(“您有”,匹配,“匹配”)
lotterynumbers=[]#重新设置数组,以便计算机可以找到新的数字列表
matches=0#每次抽奖都将比赛重新设置为0
timesplayed=timesplayed+1
我认为问题在于这些行:
if usernumbers[counter] in lotterynumbers:
matches = matches + 1 #check computer numbers against the user numbers
由于这是在生成计算机编号的循环中,因此只会根据早期计算机编号检查早期用户编号。尝试替换为:
if compnum in usernumbers:
matches = matches + 1
例如,“是计算机在用户号码列表中任意位置选择的号码”。我认为问题在于这些行:
if usernumbers[counter] in lotterynumbers:
matches = matches + 1 #check computer numbers against the user numbers
由于这是在生成计算机编号的循环中,因此只会根据早期计算机编号检查早期用户编号。尝试替换为:
if compnum in usernumbers:
matches = matches + 1
例如,“是计算机在用户号码列表中任意位置选择的号码”。问题是,在计算机选择全部六个号码之前,您检查了用户的号码。例如,当你检查用户号码[2]时,计算机到目前为止只选择了3个号码;如果用户的选择与后面的一个数字匹配,您将错过它 使用两个单独的循环:
for count in range(6):
compnum = random.randint(1,59) # choose a number between 1 and 59
while compnum in lotterynumbers:
compnum = random.randint(1,59)
lotterynumbers.append(compnum)
matches = 0
for count in range(6):
if usernumbers[count] in lotterynumbers:
matches += 1
当您学习更多Python时,有很多事情可以做得更好,但这应该可以帮助您克服眼前的问题
肾盂法
computer_numbers = random.sample(range(1,60), 6)
matches = sum([user_numbers[i] in computer_numbers for i in range(6)])
一旦你输入了全部六个用户号码,你就可以像这样抽奖和检查了。请注意,如果您愿意,可以将其折叠为一行。问题在于,在计算机选择全部六个数字之前,您检查了用户的数字。例如,当你检查用户号码[2]时,计算机到目前为止只选择了3个号码;如果用户的选择与后面的一个数字匹配,您将错过它 使用两个单独的循环:
for count in range(6):
compnum = random.randint(1,59) # choose a number between 1 and 59
while compnum in lotterynumbers:
compnum = random.randint(1,59)
lotterynumbers.append(compnum)
matches = 0
for count in range(6):
if usernumbers[count] in lotterynumbers:
matches += 1
当您学习更多Python时,有很多事情可以做得更好,但这应该可以帮助您克服眼前的问题
肾盂法
computer_numbers = random.sample(range(1,60), 6)
matches = sum([user_numbers[i] in computer_numbers for i in range(6)])
一旦你输入了全部六个用户号码,你就可以像这样抽奖和检查了。请注意,如果您愿意,您可以将其折叠为一行。如果usernum>59或usernum<1,也可以使用
缩进,我认为缩进应该更多,以便进入while循环。是的,我知道,缩进在我的实际程序中都是正确的,但是当我把它粘贴到那里的时候,它有些奇怪如果usernum>59或usernum<1:
应该缩进更多,我想进入while循环是的,我知道,缩进在我的实际程序中是正确的,但是当我把它粘贴到那里的时候,它有些奇怪,如果它在同一个位置,这并不重要,但OP正在检查所有中奖号码是否都已确定。不过,您的解决方案运行良好。它是否处于同一位置并不重要,但OP会在所有中奖号码绘制之前进行检查。但是,您的解决方案很好。对,我们不应该重新发明轮子。对,我们不应该重新发明轮子。