import random#imports random
#Author:
#File name: Lab01.py
#Date: 1/30/2014
#Purpose: To get information from the user and supply other functions with info
#Inputs: None
#Outputs: Bracket display, progress, and instructions
def main():
wordList=["max","cat","basket","ship","earth","coat","ocean"]#creates set of words for game to chose from
theWord=wordList[random.randint(0,len(wordList))]
theWordList=[]
for i in range (len(theWord)):
theWordList.append(theWord[i])#creates list of each character of the word
brickDisplay=[]
for i in range (len(theWordList)):
brickDisplay.append("-")#Creates the same number of underscores that there are letters in the word
count=0
statementCount=0
wordEnd="go"
conditionEnd="go"
userAnswer=raw_input("Type 1 or any button to play, 2 to quit")
while userAnswer != "2":
print brickDisplayMaker(brickDisplay)
letterChoice=raw_input("Enter a letter: ")
while wordEnd!="stop" or conditionEnd!= "stop" !=theWordList:
for i in range(len(theWordList)):
if theWordList[i]==letterChoice:
count=count+1#counts the number of times letterChoice was in the word
brickDisplay[i]=letterChoice
if count<1:
statementCount=statementCount+1
statementProducer(statementCount)
conditionEnd=conditionChecker(statementCount)
wordEnd=wordChecker(theWordList,brickDisplay)
print brickDisplayMaker(brickDisplay)
letterChoice=raw_input("Enter a letter: ")
count=0
userAnswer=raw_input("Type 1 or any button to play, 2 to quit")
print "Thanks for playing"
def wordChecker(theWordList,brickDisplay):
count=0
for i in range(len(theWordList)):
if theWordList[i]!=brickDisplay[i]:
count=count+1
if count==0:
return "stop"
else:
return "go"
def conditionChecker(statementCount):
if statementCount==6:
return "stop"
else:
return "go"
#Purpose: Create the brickDisplay with spaces between the underscores
#Inputs: brickDisplay
#Outputs:bricksDisplay with spaces between the underscores
def brickDisplayMaker(brickDisplay):
officialBrickDisplay=""
for ndx in brickDisplay:
officialBrickDisplay=officialBrickDisplay+ndx+" "#creates new list with spaces between underscores
return officialBrickDisplay
#Purpose: Produces statement based on the statementCount
#Inputs: statementCount
#Outputs: Statement
def statementProducer(statementCount):
if statementCount ==1:
print "draw body part: head"
elif statementCount==2:
print "draw body part: body"
elif statementCount==3:
print "draw body part: left arm"
elif statementCount==4:
print "draw body part: right arm"
elif statementCount==5:
print "draw body part: left leg"
else:
print "draw body part: right leg. You have lost"
main()
它不会退出第二个while循环。我的老师说你不能比较列表,但我认为你可以,我不认为这是问题,因为它仍然无法工作时statementCount = 6。我可以找到解决方法,但我很好奇为什么这不起作用。显然这是一个刽子手游戏。
这是一个工作代码...我正在寻找的答案是,在while语句中它应该是“和”而不是“或”
import random#imports random
#Author:
#File name: Lab01.py
#Date: 1/30/2014
#Purpose: allow user to play the game of hangman
#inputs:none
#outputs: Text that resembles the game of hangman
def main():
wordList=["max","cat","basket","ship","earth","coat","ocean"]#creates set of words for game to chose from
userAnswer=raw_input("Type 1 or any button to play, 2 to quit")
while userAnswer != "2":
theWordList=[]
theWord=wordList[random.randint(0,len(wordList)-1)]
for i in range (len(theWord)):
theWordList.append(theWord[i])#creates list of each character of the word
brickDisplay=[]
for i in range (len(theWordList)):
brickDisplay.append("-")#Creates the same number of underscores that there are letters in the word
count=0#sets count to 0
statementCount=0#sets statement count to 0
print brickDisplayMaker(brickDisplay)
letterList=[]
letterChoice=raw_input("Enter a letter: ")
letterList.append(letterChoice)
for i in range(len(theWordList)-1):
if theWordList[i]==letterChoice:#checks for letterchoice in the word
brickDisplay[i]=theWordList[i]#adds it to the brick display
count=count+1
if count==0:#check is statement count is 0
statementCount=statementCount+1
statementProducer(statementCount,theWord)
conditionEnd=conditionChecker(statementCount)#updates conditionEnd
wordEnd=wordChecker(theWordList,brickDisplay)#updates wordEnd
count=0#resets count to 0
print brickDisplayMaker(brickDisplay)
while wordEnd != "stop" and conditionEnd != "stop":
letterChoice=raw_input("Enter a letter: ")
if letterChoice in letterList: #Checks if letter has already been entered by user
print "You've already entered that"
else:
letterList.append(letterChoice)
for i in range(len(theWordList)):
if theWordList[i]==letterChoice:
count=count+1#counts the number of times letterChoice was in the word
brickDisplay[i]=theWordList[i]
print brickDisplayMaker(brickDisplay)
if count==0:
statementCount=statementCount+1
statementProducer(statementCount,theWord)
conditionEnd=conditionChecker(statementCount)
wordEnd=wordChecker(theWordList,brickDisplay)
count=0
if wordEnd=="stop":
print "You've Won!"
userAnswer=raw_input("Type 1 or any button to play, 2 to quit")
print "Thanks for playing"
#Purpose: Check if words are the same
#Inputs: theWordList, brickDisplay
#outputs: "stop" or "go"
def wordChecker(theWordList,brickDisplay):
count=0
for i in range(len(theWordList)):
if theWordList[i]!=brickDisplay[i]:
count=count+1
if count==0:
return "stop"
else:
return "go"
#Purpose: Check if statement count equals 6
#inputs: StatementCount
#outputs: "stop" or "go"
def conditionChecker(statementCount):
if statementCount==6:
return "stop"
else:
return "go"
#Purpose: Create the brickDisplay with spaces between the underscores
#Inputs: brickDisplay
#Outputs:bricksDisplay with spaces between the underscores
def brickDisplayMaker(brickDisplay):
officialBrickDisplay=""
for ndx in brickDisplay:
officialBrickDisplay=officialBrickDisplay+ndx+" "#creates new list with spaces between underscores
return officialBrickDisplay
#Purpose: Produces statement based on the statementCount
#Inputs: statementCount
#Outputs: Statement
def statementProducer(statementCount,theWord):
if statementCount ==1:
print "Draw body part: head"
elif statementCount==2:
print "Draw body part: body"
elif statementCount==3:
print "Draw body part: left arm"
elif statementCount==4:
print "Draw body part: right arm"
elif statementCount==5:
print "Draw body part: left leg"
else:
print "Draw body part: right leg. You have lost, the word was "+theWord
main()
现在是一个更新的编辑工作我的编码...
import random#imports random
#Author:
#File name: Lab01.py
#Date: 1/30/2014
#Purpose: allow user to play the game of hangman
#inputs:none
#outputs: Text that resembles the game of hangman
def main():
wordList=["max","cat","basket","ship","earth","coat","ocean","beach"]#creates set of words for game to chose from
userAnswer=raw_input("Type 1 or any button to play, 2 to quit: ")
while userAnswer != "2":
theWord=wordList[random.randint(0,len(wordList)-1)]
theWordList=[]
brickDisplay=[]
letterList=[]
count=0#sets count to 0
statementCount=0#sets statement count to 0
for i in range (len(theWord)):
theWordList.append(theWord[i])#creates list of each character of the word
brickDisplay=[]
for i in range (len(theWordList)):
brickDisplay.append("-")#Creates the same number of underscores that there are letters in the word
print brickDisplayMaker(brickDisplay)
letterChoice=raw_input("Enter a letter: ")
conditionEnd=conditionChecker(statementCount)#updates conditionEnd
wordEnd=wordChecker(theWordList,brickDisplay)#updates wordEnd
while wordEnd != "stop" and conditionEnd != "stop":
while letterChoice in letterList: #Checks if letter has already been entered by user
print "You've already entered that"
letterChoice=raw_input("Enter a letter: ")
else:
letterList.append(letterChoice)
for i in range(len(theWordList)):
if theWordList[i]==letterChoice:
count=count+1#counts the number of times letterChoice was in the word
brickDisplay[i]=theWordList[i]
print brickDisplayMaker(brickDisplay)
if count==0:
statementCount=statementCount+1
statementProducer(statementCount,theWord)
conditionEnd=conditionChecker(statementCount)
wordEnd=wordChecker(theWordList,brickDisplay)
count=0
if wordEnd=="stop":
print "You've Won!"
elif conditionEnd=="stop":
print "You have lost, the word was "+theWord #lets user know what the word was
else:
letterChoice=raw_input("Enter a letter: ")
userAnswer=raw_input("Type 1 or any button to play, 2 to quit: ")
print "Thanks for playing" #exists main
#Purpose: Check if words are the same
#Inputs: theWordList, brickDisplay
#outputs: "stop" or "go"
def wordChecker(theWordList,brickDisplay):
count=0
for i in range(len(theWordList)):
if theWordList[i]!=brickDisplay[i]:
count=count+1
if count==0:
return "stop"
else:
return "go"
#Purpose: Check if statement count equals 6
#inputs: StatementCount
#outputs: "stop" or "go"
def conditionChecker(statementCount):
if statementCount==6:
return "stop"
else:
return "go"
#Purpose: Create the brickDisplay with spaces between the underscores
#Inputs: brickDisplay
#Outputs:bricksDisplay with spaces between the underscores
def brickDisplayMaker(brickDisplay):
officialBrickDisplay=""
for ndx in brickDisplay:
officialBrickDisplay=officialBrickDisplay+ndx+" "#creates new list with spaces between underscores
return officialBrickDisplay
#Purpose: Produces statement based on the statementCount
#Inputs: statementCount
#Outputs: Statement
def statementProducer(statementCount,theWord):
if statementCount ==1:
print "Draw body part: head"
elif statementCount==2:
print "Draw body part: body"
elif statementCount==3:
print "Draw body part: left arm"
elif statementCount==4:
print "Draw body part: right arm"
elif statementCount==5:
print "Draw body part: left leg"
else:
print "Draw body part: right leg"
main()
答案 0 :(得分:0)
更换线 而userAnswer!= 2:
同
而userAnswer!=“2”:
答案 1 :(得分:0)
我决定完全编辑你的程序,因为有些事情可能会出错,我宁愿向你展示一种更简单的方法,然后尝试不断修补你的旧代码。顺便说一句,这将是一个很长的答案。
这是完整的工作计划。我在Python 3.3中写道,因为这就是我在我的机器上所拥有的,但我不认为除了print
和input
之外,Python 2.x应该有什么不同。如果您有任何问题,请告诉我。
这一行:
while wordEnd!="stop" or conditionEnd!= "stop" !=theWordList:
有一些问题。您错过了or
和最后一个语句的一半。您需要将其更改为:
while wordEnd != "stop" or conditionEnd != "stop" or something_else != theWordList:
看着我会重新考虑你是否需要while循环。
import random
# Author:
# File name: Lab01.py
# Date: 1/30/2014
# Purpose: To get information from the user and supply other functions with info
# Inputs: None
# Outputs: Bracket display, progress, and instructions
def main():
word_list = ["max", "cat", "basket", "ship", "earth", "coat", "ocean"] # creates set of words for game to chose from
word = random.choice(word_list) # chooses a random word form the list
brick_display = ['_'] * len(word) # Creates the same number of underscores that there are letters in the word
guesses = [] # tracks the letters the user has used
count = 0
wrong = 0
while True:
print(' '.join(brick_display))
print(guesses)
print(word)
try:
guess = input('Please enter a letter >> ')
if guess not in guesses:
index = check_guess(guess, word)
if index >= 0:
brick_display[index] = guess
else:
wrong += 1
statement_producer(wrong)
else:
print('You already guessed that letter')
if guess == 'exit':
break
count += 1
guesses.append(guess)
if count >= len(word):
if check_for_win(word, brick_display):
print('You win!!')
again = int(input('Play again? 1 for YES, 2 for NO'))
word = random.choice(word_list)
brick_display = ['_'] * len(word)
guesses = []
count = 0
wrong = 0
if again == 1:
pass
else:
break
elif wrong == 6:
print('You lose!!')
again = int(input('Play again? 1 for YES, 2 for NO'))
if again == 1:
pass
else:
break
except ValueError:
print('Please enter a letter')
print("Thanks for playing")
#Purpose: Checks if the guess is in the word
#Inputs: guess, word
#Outputs: If true: index of correct guess else: -1
def check_guess(guess, word):
for i in range(len(word)):
if guess == word[i]:
return i
return -1
#Purpose: Checks if the user has correctly guessed the word
#Inputs: word, guess
#Outputs:True or False
def check_for_win(word, guess):
if ''.join(guess) == word:
return True
return False
#Purpose: Produces statement based on the number of wrong guesses
#Inputs: wrong
#Outputs: Statement
def statement_producer(wrong):
if wrong == 1:
print("draw body part: head")
elif wrong == 2:
print("draw body part: body")
elif wrong == 3:
print("draw body part: left arm")
elif wrong == 4:
print("draw body part: right arm")
elif wrong == 5:
print("draw body part: left leg")
else:
print("draw body part: right leg. You have lost")
main()