回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>好吧,我正在为我的CSCI课程做最后一个项目,我决定用一个刽子手程序,因为这是一个相当简单的游戏来实现。我的一切工作都很正常,除了我遇到的唯一问题是我的一些打印声明。代码如下:</p>
<pre><code>import random
import os
HANGMANPICS = ['''
+---+
| |
|
|
|
|
|
=========== ''','''
+---+
| |
O |
|
|
|
|
=========== ''','''
+---+
| |
O |
| |
|
|
|
=========== ''','''
+---+
| |
O |
/| |
|
|
|
=========== ''','''
+---+
| |
O |
/|\ |
|
|
|
=========== ''','''
+---+
| |
O |
/|\ |
/ |
|
|
=========== ''','''
+---+
| |
O |
/|\ |
/ \ |
|
|
=========== ''']
ANIMALS = '''cat dog fish whale otter spider snake bird dolphin tiger mouse
rabbit bear lion'''
CITIES = '''indianapolis chicago orlando miami denver columbus memphis oakland
seatlle phoenix dallas detroit baltimore cincinatti'''
COUNTRIES = '''india china america japan egypt greece mexico italy canada
australia france brazil germany korea'''
def Countries():
print 'Guess the name of this country'
return COUNTRIES
def Cities():
print 'Guess the name of this city'
return CITIES
def Animals():
print 'Guess the name of this animal'
return ANIMALS
def Random():
print 'Guess the name of this randomly chosen country, city, or animal'
return COUNTRIES + CITIES + ANIMALS
def Welcome():
print ('Select a category:')
print ('1: Countries')
print ('2: Cities')
print ('3: Animals')
print ('4: Random')
choice = {"1": Countries, "2": Cities, "3": Animals, "4": Random}
choose = raw_input()
return choice.get(choose, Random)().split()
def getRandomword(wordlist):
wordindex = random.randint(0, len(wordlist)-1)
return wordlist[wordindex]
def Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD):
os.system('cls')
print(HANGMANPICS[len(MISSEDLETTERS)])
print
print 'Missed letters:',
for letter in MISSEDLETTERS:
print letter,
print
blanks = '_' * len(SECRETWORD)
for i in range(len(SECRETWORD)):
if SECRETWORD[i] in CORRECTLETTERS:
blanks = blanks[:i] + SECRETWORD[i] + blanks[i+1:]
for letter in blanks:
print letter
print
def getGuess(alreadyguessed):
while True:
print 'Guess a letter'
guess = raw_input()
guess = guess.lower()
if len(guess) != 1:
print 'Please enter single letters'
elif guess in alreadyguessed:
print 'This letter has already been guessed, please guess again'
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print 'You did not guess a letter, please guess a letter'
else:
return guess
def playAgain():
print ('Do you wanna play again? (Yes or No)')
return raw_input().lower().startswith('y')
print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)
done = False
while True:
Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
guess = getGuess(MISSEDLETTERS + CORRECTLETTERS)
if guess in SECRETWORD:
CORRECTLETTERS = CORRECTLETTERS + guess
found = True
for i in range(len(SECRETWORD)):
if SECRETWORD[i] not in CORRECTLETTERS:
found = False
break
if found:
print('You won the game!')
print('The correct word was ---->' + SECRETWORD.upper())
done = True
else:
MISSEDLETTERS = MISSEDLETTERS + guess
if len(MISSEDLETTERS) == len(HANGMANPICS)-1:
Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
print('You did not guess the word correctly. The word was : ' + SECRETWORD)
done = True
if done:
if playAgain():
os.system('cls')
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
done = False
SECRETWORD = getRandomword(words)
else:
break
</code></pre>
<p>我希望得到打印声明在国家,城市,动物,并随机打印在实际的游戏屏幕后,作出了选择,但我没有任何运气,它的工作,如果我把操作系统('cls'),但它们有助于使程序看起来更好,这也是一个优点。太好了!谢谢!在</p>