[英]Python multiple number guessing game
I am trying to create a number guessing game with multiple numbers. 我正在尝试创建一个具有多个数字的猜数字游戏。 The computer generates 4 random numbers between 1 and 9 and then the user has 10 chances to guess the correct numbers.
计算机会生成1到9之间的4个随机数,然后用户有10次机会猜测正确的数字。 I need the feedback to display as
YYYY
for 4 correct numbers guessed, YNNY
for first and last number guessed etc. (you get the point). 我需要将反馈显示为
YYYY
才能猜出4个正确数字, YNNY
可以猜出第一个和最后一个数字,等等(您明白了)。 the code below keeps coming back saying IndexError: list index out of range
. 下面的代码不断返回,提示
IndexError: list index out of range
。
from random import randint
guessesTaken = 0
randomNumber = []
for x in range(4):
tempNumber = randint(1, 9)
randomNumber.append(tempNumber)
Guess = []
Guess.append(list(input("Guess Number: ")))
print(randomNumber)
print(Guess)
if randomNumber[0] == Guess[0]:
print("Y")
elif randomNumber[1] == Guess[1]:
print("Y")
elif randomNumber[2] == Guess[2]:
print("Y")
elif randomNumber[3] == Guess[3]:
print("Y")
elif randomNumber[0] != Guess[0]:
print("N")
elif randomNumber[1] != Guess[1]:
print("N")
elif randomNumber[2] != Guess[2]:
print("N")
elif randomNumber[3] != Guess[3]:
print("N")
Right now you're only asking the user for one guess, and appending the guess to the Guess
list. 现在,您只要求用户进行一次猜测,然后将猜测追加到“
Guess
列表中。 So the Guess
list has one element, but you're using Guess[1]
, Guess[2]
, etc., which of course results in the IndexError
所以
Guess
列表只有一个元素,但是您使用的是Guess[1]
, Guess[2]
等,这当然会导致IndexError
You need four guesses to match for random numbers, you can also shorted your code using a list comp: 您需要四个猜测来匹配随机数,也可以使用列表组合来缩短代码:
from random import randint
guessesTaken = 0
randomNumber = []
Guess = []
for x in range(4):
tempNumber = str(randint(1, 9)) # compare string to string
randomNumber.append(tempNumber)
Guess.append(input("Guess Number: "))
print("".join(["Y" if a==b else "N" for a,b in zip(Guess,randomNumber)]))
You can also use enumerate to check elements at matching indexes: 您还可以使用枚举来检查匹配索引处的元素:
print("".join(["Y" if randomNumber[ind]==ele else "N" for ind, ele in enumerate(Guess)]))
To give the user guesses in a loop: 要给用户一个循环的猜测:
from random import randint
guessesTaken = 0
randomNumber = [str(randint(1, 9)) for _ in range(4)] # create list of random nums
while guessesTaken < 10:
guesses = list(raw_input("Guess Number: ")) # create list of four digits
check = "".join(["Y" if a==b else "N" for a,b in zip(guesses,randomNumber)])
if check == "YYYY": # if check has four Y's we have a correct guess
print("Congratulations, you are correct")
break
else:
guessesTaken += 1 # else increment guess count and ask again
print(check)
I'll rearrange your code a bit, so it doesn't stray too far from what you've done. 我将对您的代码进行一些重新排列,以免与您所做的工作相差太远。
from random import randint
guessesTaken = 0
randomNumbers = []
Guess = [] # Combine your guesses with your loop
for x in range(4):
tempNumber = randint(1, 9)
randomNumbers.append(tempNumber)
# This should be done four times too
# In Python 2, instead of this:
# Guess.append(input("Guess Number: "))
# do this:
Guess.append(int(raw_input("Guess Number: "))) # raw_input and pass to int
# in python 3, raw_input becomes input, so do this instead:
# Guess.append(int(input("Guess Number: ")))
print(randomNumbers)
print(Guess)
You can combine these in a loop to avoid the repetitive code: 您可以将它们组合成一个循环,以避免重复的代码:
if randomNumbers[0] == Guess[0]:
print("Y")
else:
print("N")
if randomNumbers[1] == Guess[1]:
print("Y")
else:
print("N")
if randomNumbers[2] == Guess[2]:
print("Y")
else:
print("N")
if randomNumbers[3] == Guess[3]:
print("Y")
else:
print("N")
Perhaps, to print your desired result eg YNNY, like this: 也许,要打印您想要的结果,例如YNNY,如下所示:
result = []
for index in range(4):
if randomNumbers[index] == Guess[index]:
result.append("Y")
else:
result.append("N")
print(''.join(result))
If you want terser code use Python's ternary operation: 如果要使用简短代码,请使用Python的三元操作:
result = []
for index in range(4):
result.append("Y" if randomNumbers[index] == Guess[index] else "N")
print(''.join(result))
Or use the fact that True == 1
and False == 0
as indexes: 或使用
True == 1
和False == 0
作为索引的事实:
result = []
for index in range(4):
result.append("NY"[randomNumbers[index] == Guess[index]])
print(''.join(result))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.