我有一个刽子手代码。它很长,你不必全部阅读(但如果你愿意,请继续):
import random
import sys
from string import whitespace
guess = ''
guess2 = 0
guess3 = 0
guess4 = 0
guess5 = 0
guess6 = 0
guess7 = 0
guess8 = 0
guess9 = 0
guess10 = 0
guess11 = 0
guess12 = 0
guess13 = 0
guess14 = 0
guess15 = 0
guess16 = 0
guess17 = 0
guess18 = 0
guess19 = 0
guess20 = 0
guess21 = 0
lis = [ ]
uy = 'jrekghqegherwgbeuihrweig'
choice = 'bhekvvvvvfaktgrwekubfhdbvbvdvbd'
z = 'bqhjrfqfbrqkqbg'
s = 'ksl'
s_space = 'bhks'
li = [ ]
a1a = None
a2a = None
a3a = None
a4a = None
a5a = None
a6a = None
a7a = None
a8a = None
a9a = None
a10a = None
a11a = None
a12a = None
a13a = None
a14a = None
a15a = None
a16a = None
a17a = None
t = list(s)
googe = r'''
/@
\ \
___> \
(__O) \
(____@) \
(____@) \
(__o)_ \
\ \
'''
comps = r'''
._________________.
|.---------------.|
|| hangman ||
|| -._ .-. ||
|| -._| | | ||
|| -._|"|"| ||
|| -._|.-.| ||
||_______________||
/.-.-.-.-.-.-.-.-.\
/.-.-.-.-.-.-.-.-.-.\
/.-.-.-.-.-.-.-.-.-.-.\
/______/__________\_____\
\_______________________/
'''
hangman = """
+---+
| |
|
|
|
|
=========
"""
hangman1 = """
+---+
| |
O |
|
|
|
=========
"""
hangman2 = """
+---+
| |
O |
| |
|
|
=========
"""
hangman3 = """
+---+
| |
O |
|/ |
|
|
=========
"""
hangman4 = """
+---+
| |
O |
\|/ |
|
|
=========
"""
hangman5 = """
+---+
| |
O |
\|/ |
| |
|
=========
"""
hangman6 = """
+---+
| |
O |
\|/ |
| |
\ |
=========
"""
hangman7 = """
+---+
| |
O |
\|/ |
| |
/ \ |
=========
Game Over.
"""
hangman0 = hangman
animals = [ 'Swan']
space = ['astronomy', 'gas', 'nebula', 'planet', 'Earth', 'sun', 'space', 'star', 'Venus', 'Mars', 'Mercury', 'Pluto', 'Neptune', 'Uranus', 'ice', 'Jupiter', 'Saturn', 'asteroid', 'comet', 'Kepler', 'ISS', 'station', 'quasar', 'galaxy', 'Andromeda', 'moon', 'Phobios', 'Demios', 'orbit', 'gravity', 'matter']
food = ['pizza', 'bagel', 'donut', 'candy', 'cake', 'casserole', 'salmon', 'bacon', 'caviar', 'apple', 'bannana', 'orange', 'acai', 'pomegranete', 'pear', 'cheese', 'turkey', 'steak', 'Cabbage', 'Calorie', 'Carbohydrate', 'Casserole', 'Caterer', 'Celery', 'Chef', 'Chewy', 'Chicken', 'Chicory', 'Chipotle', 'Chips', 'Chocolate', 'Chocolatier', 'Choice', 'Chutney', 'Cocoa', 'Coffee', 'Condiment', 'mango', 'Crackers', 'Croutons', 'Crunchy', 'Cucumbers', 'potato', 'sandwich']
chemicals = ['alum', 'sodium', 'titin', 'Bichrome', 'Azurite', 'Borax', 'Calomel', 'Gypsum', 'graphite', 'hypo', 'lime', 'acetone', 'pollen', 'Aluminium', 'starch', 'soap', 'calcium', 'alchohol', 'seltzer', 'aspirin', 'bronze', 'chalk', 'diamond', 'marble', 'quartz', 'sand', 'silicon', 'vinegar', 'carbon', 'nitrogen', 'vapor', 'oxide', 'phosphorus', 'magnesium', 'acid', 'chemical', 'ackey']
print 'Welcome to hangman!'
import time
time.sleep(3)
print 'Pick a topic:'
print '1.Animals'
print '2.Space'
print '3.Food'
print '4.Chemicals'
topic = str(raw_input('Choose a topic(1/2/3/4):'))
if not topic.isalpha():
if topic == '1':
choice = animals
print 'animals'
elif topic == '2':
choice = space
print 'space'
elif topic == '3':
choice = food
print 'food'
elif topic == '4':
choice = chemicals
print 'chemicals'
else:
print 'Invalid Input'
topic = str(raw_input('Choose a topic(1/2/3/4):'))
elif topic != '1' or '2' or '3' or '4':
print 'Invalid Input'
topic = str(raw_input('Choose a topic(1/2/3/4):'))
if choice == animals:
word = random.choice(animals)
if choice == food:
word = random.choice(food)
if choice == space:
word = random.choice(space)
if choice == chemicals:
word = random.choice(chemicals)
word = word.lower()
length = len(word)
if length == 3:
s = '_ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
lis = [a1a, a2a, a3a]
if length == 4:
s = '_ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
lis = [a1a, a2a, a3a, a4a]
if length == 5:
s = '_ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
lis = [a1a, a2a, a3a, a4a, a5a]
if length == 6:
s = '_ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
lis = [a1a, a2a, a3a, a4a, a5a, a6a]
if length == 7:
s = '_ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
list = [a1a, a2a, a3a, a4a, a5a, a6a, a7a]
if length == 8:
s = '_ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a]
if length == 9:
s = '_ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a]
if length == 10:
s = '_ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
a10a = word[9]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a]
if length == 11:
s = '_ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a= word[8]
a10a = word[9]
a11a = word[10]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a]
if length == 12:
s = '_ _ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
a10a = word[9]
a11a = word[10]
a12a = word[11]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a]
if length == 13:
s = '_ _ _ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
a10a = word[9]
a11a = word[10]
a12a = word[11]
a13a = word[12]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a, a13a]
if length == 14:
s = '_ _ _ _ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a= word[7]
a9a= word[8]
a10a = word[9]
a11a = word[10]
a12a = word[11]
a13a = word[12]
a14a = word[13]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a, a13a, a14a]
if length == 15:
s = '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
a10a = word[9]
a11a = word[10]
a12a = word[11]
a13a = word[12]
a14a = word[13]
a15a = word[14]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a, a13a, a14a, a15a]
if length == 16:
s = '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
a10a = word[9]
a11a = word[10]
a12a = word[11]
a13a = word[12]
a14a = word[13]
a15a = word[14]
a16a = word[15]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a, a13a, a14a, a15a, a16a]
if length == 17:
s = '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _'
a1a = word[0]
a2a = word[1]
a3a = word[2]
a4a = word[3]
a5a = word[4]
a6a = word[5]
a7a = word[6]
a8a = word[7]
a9a = word[8]
a10a = word[9]
a11a = word[10]
a12a = word[11]
a13a = word[12]
a14a = word[13]
a15a = word[14]
a16a = word[15]
a17a = word[16]
lis = [a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a, a13a, a14a, a15a, a16a, a17a]
while hangman != hangman7 or s_space != word:
print """
"""
print hangman
print s
guess = str(raw_input('Enter a letter:'))
if guess20 == guess:
guess21 = guess
if guess19 == guess:
guess20 = guess
if guess18 == guess:
guess19 = guess
if guess17 == guess:
guess18 = guess
if guess16 == guess:
guess17 = guess
if guess15 == guess:
guess16 = guess
if guess14 == guess:
guess15 = guess
if guess13 == guess:
guess14 = guess
if guess12 == guess:
guess13 = guess
if guess11 == guess:
guess12 = guess
if guess10 == guess:
guess11 = guess
if guess9 == guess:
guess10 = guess
if guess8 == guess:
guess9 = guess
if guess7 == guess:
guess8 = guess
if guess6 == guess:
guess7 = guess
if guess5 == guess:
guess6 = guess
if guess4 == guess:
guess5 = guess
if guess3 == guess:
guess4 = guess
if guess2 == guess:
guess3 = guess
if guess == guess:
guess2 = guess
li = [guess, guess2, guess3, guess4, guess5, guess6, guess7, guess8, guess9, guess10, guess11, guess12, guess13, guess14, guess15, guess16, guess17, guess18, guess19, guess20, guess21]
if guess.isalpha():
if not guess in lis:
print 'Try again'
inc = guess
if hangman == hangman6:
hangman = hangman7
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
print hangman7
sys.exit(" ")
elif hangman == hangman5:
hangman = hangman6
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman4:
hangman = hangman5
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman3:
hangman = hangman4
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman2:
hangman = hangman3
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman1:
hangman = hangman2
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman0:
hangman = hangman1
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
else:
gue = 'hi'
if guess in lis:
if guess == a1a:
correct = a1a
t = list(s)
t[0] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a2a:
correct = a2a
t = list(s)
t[2] = guess
s = "".join(t)
print hangman
print guess
s_0 = s.replace(" ","")
elif guess == a3a:
correct = a3a
t = list(s)
t[4] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a4a:
correct = a4a
t = list(s)
t[6] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a5a:
correct = a5a
t = list(s)
t[8] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a6a:
correct = a6a
t = list(s)
t[10] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a7a:
correct = a7a
t = list(s)
t[12] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a8a:
correct = a8a
t = list(s)
t[14] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a9a:
correct = a9a
t = list(s)
t[16] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a10a:
correct = a10a
t = list(s)
t[18] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a11a:
correct = a11a
t = list(s)
t[20] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a12a:
correct = a12a
t = list(s)
t[22] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a13a:
correct = a13a
t = list(s)
t[24] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a14a:
correct = a14a
t = list(s)
t[26] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a15a:
correct = a15a
t = list(s)
t[28] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a16a:
correct = a16a
t = list(s)
t[30] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
elif guess == a17a:
correct = a17a
t = list(s)
t[32] = guess
s = "".join(t)
print guess
s_0 = s.replace(" ","")
print hangman
print s
if s_0 == word:
print s_0
print 'You Won!'
print googe
import time
time.sleep(3)
print ' This program was written using Python, by Slass33'
print comps
break
co = raw_input('Do you want to play again?(yes/no):')
if co == 'yes':
print """
"""
elif co == 'no':
sys.exit
else:
co = raw_input('Do you want to play again?(yes/no):')
sys.exit
所以,它工作正常(留出一些小故障)。但我的问题是,我希望它显示rong
列表下的所有先前错误的猜测,但它不起作用。有什么想法?这个主要部分是:
if not guess in lis:
print 'Try again'
inc = guess
if hangman == hangman6:
hangman = hangman7
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
print hangman7
sys.exit(" ")
elif hangman == hangman5:
hangman = hangman6
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman4:
hangman = hangman5
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman3:
hangman = hangman4
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman2:
hangman = hangman3
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman1:
hangman = hangman2
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
elif hangman == hangman0:
hangman = hangman1
incorrect = inc in li
rong = []
rong.append(incorrect)
print s
print 'incorrect:'
print rong
所以基本上,如果猜测!=
该词的任何索引,则显示一个版本的刽子手string
,其余部分是已知的。其中包含var
的任何hangman
都是显示内容。
任何人都可以为我解决这个问题,或者改进这段代码吗?
P.S。忽略animals[ ]
只有'Swan'
的事实。如果你不知道我在说什么,请忽略这一点。
答案 0 :(得分:3)
您不应将空列表重新分配到rong
块内的if...elif
,这会导致rong
列表中的所有先前值都消失。
你应该在游戏开始前初始化rong
(可能有一段时间或for循环,你没有包括这是游戏的主要部分),你应该初始化rong
到那里的[]
并且只在if块中附加到该列表。
示例 -
rong = []
while <condition>: #Game loop?
.
.
.
if not guess in lis:
.
.
if hangman == hangman6:
.
#rong = [] #Do not do this.
rong.append(incorrect) #Just append to 'rong' .