[英]simple Python game doesn't work no matter what
I been trying to write a Python program that implements the cows bulls game, here's my code: 我一直在尝试编写一个实现牛牛游戏的Python程序,这是我的代码:
import random
def cb():
pc = random.sample('123456789',4)
cows = []
bulls = []
while True:
guess = input('please enter a 4 digit num: ')
cows = rmd(cows)
bulls = rmd(bulls)
if pc == guess:
print('You win!')
break
(cows.append(i) for i in pc if i in guess and guess.index(i) == pc.index(i))
(bulls.append(i) for i in pc if i in guess and guess.index(i) != pc.index(i))
print ('{} cows and {} bulls'.format(len(cows), len(bulls))
continue
Just the last approach I tried before giving up, before that I wrote more than 50 programs and nothing would work, it would always return 0 cows 0 bulls. 这是我放弃之前尝试的最后一种方法,在此之前我编写了50多个程序,但没有任何效果,它将始终返回0牛0公牛。 I have tried for loops while guess!=pc among hundreds of ideas.. Nothing would work.
我已经尝试过循环,而在数百个想法中都猜到了!= pc。
rmd is a function that removes duplicate items from a given list by converting it to a set and then converting that set to a list and returns the final list. rmd是一项功能,该功能可通过将给定列表转换为集合然后将其转换为列表并返回最终列表来从给定列表中删除重复项。
It's just a silly game but the fact that nothing works is very depressing.. 这只是一个愚蠢的游戏,但没有任何效果的事实令人非常沮丧。
random.sample
returns a list, so it will never be equal to the string the user enters. random.sample
返回一个列表,因此它永远不会等于用户输入的字符串。
The lines (cows.append(i)...
and (bulls.append(i)...
are generators, meaning that they do nothing unless and until they are evaluated. You could technically turn them into list comprehensions by swapping the enclosing parentheses for square brackets, but you would be building up a list for its side effects, which is frowned upon. Instead of putting a list.append
in a generator or comprehension, make a comprehension that actually builds the list you want, as they're intended to do. Additionally, since you only want the unique items in the first place, there's no reason to use lists at any point. 这些行
(cows.append(i)...
和(bulls.append(i)...
是生成器,这意味着除非进行评估,否则它们什么也不做。您可以通过交换该封围从技术上将它们转换为列表理解方括号括起来,但是您会为其副作用建立一个列表,这很令人皱眉。与其将list.append
在生成器或理解中, list.append
使其真正构建您想要的列表,因为它们是另外,由于您只希望唯一的项,因此没有理由在任何时候使用列表。
There's also no reason to build lists or sets at all, since all you want to do is count up each instance. 也根本没有理由构建列表或集合,因为您要做的就是计算每个实例。
import random
def cb():
pc = ''.join(random.sample('123456789',4))
while True:
guess = input('please enter a 4 digit num: ')
if pc==guess:
print ('You win!')
break
bulls = 0
cows = 0
for character in guess:
if character in pc:
if guess.index(character) == pc.index(character):
bulls += 1
else:
cows += 1
print(cows, 'cows and', bulls, 'bulls')
print
often to see what's in your variables, you'd be surprised! print
查看变量中的内容,您会感到惊讶! random.sample('123456789',4)
returns a list of 4 letters, not a 4 letter word. random.sample('123456789',4)
返回4个字母的列表 ,而不是4个字母的单词。 input
, as opposed to raw_input, attempts automatic conversions (for example entering "1234" would result in an int) and some consider it bad practice. input
尝试进行自动转换(例如,输入“ 1234”将导致一个int),并且有人认为这是不好的做法。 Use raw_input to ensure you get a string. "".join(random.sample....)
how can they be equal?! "".join(random.sample....)
正确地连接了它,那么它们怎么相等?! Code: 码:
for index,i in enumerate(guess):
if i in guess:
if pc[index] == i: cows.append(i)
else: bulls.append(i)
Good luck! 祝好运! EDIT:
编辑:
I saw someone solved it for you - I still urge you to read about generators, and maybe a walkthrough. 我看到有人为您解决了该问题-我仍然敦促您阅读有关发电机的内容,也许还会有一个演练。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.