所以我一直在讨论这个问题,在搜索了其他多个问题之后找不到我需要的答案。
我已经设置了一个4位数随机数生成器的任务,然后用户必须尝试猜测数字。他们每次尝试都会说出他们得到了多少数字以及他们所处的位置。 此数字也需要处于正确的位置。
这是我目前的代码。
import random as r
def GetNumber():
number = [r.randint(0, 9), r.randint(0, 9), r.randint(0, 9), r.randint(0, 9)]
return number
def Choices():
randomNumber = GetNumber()
userChoice = list(input("Enter 4 numbers\n")) #Allows the user to input a number 4 digits long
userChoice = [int(i) for i in userChoice]
if userChoice == randomNumber:
print("Congratulations! You chose the right number")
提前致谢:)
答案 0 :(得分:1)
您可以zip
随机数和用户的选择并比较两对:
>>> number = [4, 3, 9, 1]
>>> choice = [1, 3, 4, 1]
>>> [n == c for n, c in zip(number, choice)]
[False, True, False, True]
>>> sum(n == c for n, c in zip(number, choice))
2
要获得匹配数字的总数,无论其位置如何,您都可以通过Counter
同时输入数字和用户的选择,并与&
进行交集:
>>> from collections import Counter
>>> Counter(number) & Counter(choice)
Counter({1: 1, 3: 1, 4: 1})
>>> sum((Counter(number) & Counter(choice)).values())
3
答案 1 :(得分:1)
这是我的看法。
import random as r
def GetNumber():
return [r.randint(0, 9) for i in range(4)]
def Choices():
randomNumber = GetNumber()
userChoice = [int(i) for i in list(input("Enter 4 numbers\n"))]
n = 0
while userChoice != randomNumber:
hits = [str(i+1) for i in range(4) if userChoice[i] == randomNumber[i]]
if hits:
print('You got position(s) {} correct'.format(', '.join(hits)))
else:
print('You got all of them wrong!')
userChoice = [int(i) for i in list(input("Enter 4 numbers\n"))]
n += 1
print("Congratulations! You found the right number in {} turns!!".format(n))
Choices()
代码已经重新构建了一段时间,直到用户实际找到了密码。这只是一个框架,您可以在其上进行一些实验,并尝试在用户交互或其他方面进一步优化它。
如果有任何不清楚的地方,请告诉我。干杯!
答案 2 :(得分:0)
我认为关键是使用一串数字而不是数字数据类型。这是一个有效的例子。你可以通过比较数字来做到这一点,但在这种情况下,我们并不关心数字值这么多的字符模式。
from random import choice
from string import digits
def is_digits(string):
"""Return true if all characters are digits, otherwise false"""
return all([char.isdigit() for char in string])
def get_number(length):
"""Return a string of digits with the number of characters equal to length"""
return ''.join(choice(digits) for i in range(length))
def guess():
"""Receive and evaluate guesses for match to randomly generated number"""
guess = ''
miss_char = '-'
miss_message = 'Try again.'
win_message = 'Congratulations! You chose the right number.'
answer_length = 4
answer = get_number(answer_length)
while guess != answer:
guess = raw_input('Enter {0} numbers: '.format(len(answer)))
if len(guess) != len(answer) or is_digits(guess) is False:
continue
matches = [answer[i]
if answer[i] == guess[i]
else miss_char
for i in range(len(answer))]
matches_string = ''.join(matches)
message_base = 'Matched digits: {0}.'.format(matches_string)
if guess != answer:
print(' '.join([message_base, miss_message]))
guess = ''
else:
print(' '.join([message_base, win_message]))
guess()
答案 3 :(得分:-1)
import random as r
def GetNumber():
number = r.randint(0000, 9999)
return str(number).zfill(4)
def Choices():
randomNumber = GetNumber()
print type(randomNumber),randomNumber
userChoice = str(input("Enter 4 numbers\n")).strip("\n") #Allows the user to input a number 4 digits long
for a,b in zip(userChoice,randomNumber):
if not a==b:
print "{0} not match {1}".format(a,b)
if userChoice==randomNumber:
print "Congratulations! You chose the right number"
Choices()