我创建了一些功能来帮助实现4针猜游戏,并带有一个while循环,该循环告诉我不断猜测序列顺序,直到它与生成的PIN匹配为止。
import random
def genListDigits(): # No.1 Generates a list with 4 random digits between 0 and 9.
myList = []
for i in range(0,4):
x = random.randint(0,9)
myList.append(x)
return myList
b = genListDigits()
def convertToList(): # No.2 Receives my pin and converts it into a 4-digit list.
convertedList = []
for s in p:
convertedList.append(int(s))
return convertedList
p = input("Enter a 4-PIN code: ")
a = convertToList()
print('you entered', a)
def checkNumbers(guess,right): # #No.3 Receives both lists, compares each element in order returns a string with 4 letters stating to go higher or lower
result = ""
for n in range(4):
if guess[n] == right[n]:
result += "Y"
elif guess[n] < right[n]:
result += "H"
elif guess[n] > right[n]:
result += "L"
return print(result)
checkNumbers(a,b)
但是,当我添加while循环时,它似乎并没有重复执行convertToList
(“输入4-PIN码:”
)函数。我的while循环:
while a != b:
q = input(convertToList())
print("Correct!")
关于如何解决这个问题的任何想法?提前致谢。
问题来源:stackoverflow
你快到了。您只需要将p
,a
放入while循环中并检查a == b
即可,如果为true,则打破循环并显示“正确猜测!”。
while True:
p = input("Enter a 4-PIN code: ")
a = convertToList()
print("you entered", a)
checkNumbers(a, b)
if a == b:
print("Correct guess!")
break
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。