Python 刽子手代码蟒蛇3
我有一个Python3 hangman代码可以工作,但我不知道如何允许用户在主函数中选择难度(easy(9)、medium(7)、hard(5))。是否有一个更简单的代码可以用于此Python 刽子手代码蟒蛇3,python,python-3.x,Python,Python 3.x,我有一个Python3 hangman代码可以工作,但我不知道如何允许用户在主函数中选择难度(easy(9)、medium(7)、hard(5))。是否有一个更简单的代码可以用于此 import random WORDLIST = ("phone", "laptop", "desktop", "sewer", "television", "never", "guess", "nice", "chair", "car"); WORD = random.choice(WORDLIST) ACCEP
import random
WORDLIST = ("phone", "laptop", "desktop", "sewer", "television", "never", "guess", "nice", "chair", "car");
WORD = random.choice(WORDLIST)
ACCEPTABLE = ("abcdefghijklmnopqrstuvwxyz")
guessed = []
state = 0
hasWon = 0
playedOnce = 0
def menu ():
print("""
1. Easy (9 misses)
2. Medium (7 misses)
3. Hard (5 misses)
""")
return int(input("What level do you want to play?:"))
def wantsToPlay():
if (not playedOnce):
return 1
l = input("\nWould you like to play again? (y/n)")
while (l != "y" and l != "Y" and l != "n" and l != "N"):
l = input("\nWould you like to play again? (y/n)")
if (l.lower() == "y"):
return 1
return 0
def takeNewLetter():
global state, hasWon
newPrint("So far, you have guessed the following letters...")
for g in guessed:
print(g, end=" ")
letter = input("\n\nWhat letter would you like to guess next?\n")
while (letter in guessed or letter not in ACCEPTABLE):
if (len(letter) > 1):
if (letter.lower() == WORD.lower()):
newPrint("You win!")
hasWon = 1
break
else:
newPrint("Boo... that was wrong... you're dead...")
state = 7
break
else:
if (letter not in ACCEPTABLE):
letter = input("That character is unacceptable. You many only enter lower case letters.\n")
else:
letter = input("You have already guessed that letter, try another one...\n")
guessed.append(letter)
if (letter not in WORD):
state += 1
return
def drawWord():
tempWord = ""
for c in WORD:
if (c in guessed):
tempWord += c + " "
else:
tempWord += "_ "
newPrint(tempWord)
return
def drawStickman():
if (state >= 7):
print(" _______")
print("|/ |")
print("| (_)")
print("| \|/")
print("| |")
print("| / \\")
print("|")
print("|___")
print("Oops. You're dead.")
elif (state == 6):
print(" _______")
print("|/ |")
print("| (_)")
print("| \|/")
print("| |")
print("| / ")
print("|")
print("|___")
elif (state == 5):
print(" _______")
print("|/ |")
print("| (_)")
print("| \|/")
print("| |")
print("|")
print("|")
print("|___")
elif (state == 4):
print(" _______")
print("|/ |")
print("| (_)")
print("| \|/")
print("|")
print("|")
print("|")
print("|___")
elif (state == 3):
print(" _______")
print("|/ |")
print("| (_)")
print("| \|")
print("|")
print("|")
print("|")
print("|___")
elif (state == 2):
print(" _______")
print("|/ |")
print("| (_)")
print("|")
print("|")
print("|")
print("|")
print("|___")
elif (state == 2):
print(" _______")
print("|/ |")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|___")
elif (state == 1):
newPrint("As this is your first mistake, I will let you off...")
print(" _______")
print("|/")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|___")
elif (state == 0):
print(" _______")
print("|/")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|___")
def hasGuessed():
if (hasWon == 1):
return 1
if (state >= 7):
return 1
for c in WORD:
if (c not in guessed):
return 0
if (len(guessed) == 0):
return 0
return 1
def setup_game():
newPrint("Welcome to the Hangman game!")
newPrint("I have chosen a random word from my super secret list, try to guess it before your stickman dies!")
def newPrint(message, both = 1):
msg = "\n" + message
if (both != 1):
msg += "\n"
print(msg)
def main():
global guessed, hasWon, state, playedOnce, WORD, WORDLIST
setup_game()
newPrint("My word is " + str(len(WORD)) + " letters long.")
while (wantsToPlay() == 1):
WORD = random.choice(WORDLIST)
guessed = []
playedOnce = 1
hasWon = 0
state = 0
while (hasGuessed() == 0 and state < 7):
drawStickman()
drawWord()
takeNewLetter()
drawStickman()
newPrint("My word was " + WORD)
main()
随机导入
单词列表=(“电话”、“笔记本电脑”、“台式机”、“下水道”、“电视”、“从不”、“猜猜”、“漂亮”、“椅子”、“汽车”);
单词=随机。选择(单词列表)
可接受=(“abcdefghijklmnopqrstuvxyz”)
猜测=[]
状态=0
哈斯旺=0
播放时间=0
def菜单():
打印(“”)
1.容易(9次失误)
2.中等(7次失误)
3.困难(5次失误)
""")
return int(输入(“您想玩什么级别?:”)
def wantsToPlay():
如果(非播放时间):
返回1
l=输入(“\n您想再次播放吗?(y/n)”)
而(l!=“y”和l!=“y”和l!=“n”和l!=“n”):
l=输入(“\n您想再次播放吗?(y/n)”)
如果(l.lower()=“y”):
返回1
返回0
def takeNewLetter():
全球国家,哈斯旺
newPrint(“到目前为止,您已经猜到了以下字母…”)
对于猜测中的g:
打印(g,end=“”)
字母=输入(“\n\n您想猜下一个字母?\n”)
while(猜中字母或不可接受字母):
如果(长度(字母)>1):
如果(字母.lower()==WORD.lower()):
newPrint(“你赢了!”)
哈斯旺=1
打破
其他:
newPrint(“嘘……那是错的……你死了……”)
州=7
打破
其他:
如果(字母不可接受):
字母=输入(“该字符不可接受。您只能输入小写字母。\n”)
其他:
字母=输入(“您已经猜到了该字母,请尝试另一个…\n”)
猜到了。追加(字母)
如果(字母非大写):
状态+=1
返回
def drawWord():
tempWord=“”
对于WORD中的c:
如果(猜测中的c):
tempWord+=c+“”
其他:
tempWord+=“389;”
新打印(临时字)
返回
def drawStickman():
如果(状态>=7):
打印(“打印”)
打印(“|/|”)
打印(“|(|)”)
打印(“\\\\/”)
打印(“| |”)
打印(“|/\ \”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
打印(“哎呀,你死定了。”)
elif(状态==6):
打印(“打印”)
打印(“|/|”)
打印(“|(|)”)
打印(“\\\\/”)
打印(“| |”)
打印(“|/”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==5):
打印(“打印”)
打印(“|/|”)
打印(“|(|)”)
打印(“\\\\/”)
打印(“| |”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==4):
打印(“打印”)
打印(“|/|”)
打印(“|(|)”)
打印(“\\\\/”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==3):
打印(“打印”)
打印(“|/|”)
打印(“|(|)”)
打印(“\\\”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==2):
打印(“打印”)
打印(“|/|”)
打印(“|(|)”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==2):
打印(“打印”)
打印(“|/|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==1):
newPrint(“因为这是你的第一个错误,我会让你离开……”)
打印(“打印”)
打印(“|/”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
elif(状态==0):
打印(“打印”)
打印(“|/”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“|”)
打印(“| uuuuuuuuuuuuuuuuuuu”)
def hasgessed():
如果(hasWon==1):
返回1
如果(状态>=7):
返回1
对于WORD中的c:
如果(c不在猜测中):
返回0
如果(len(猜测)==0):
返回0
返回1
def设置_游戏():
newPrint(“欢迎来到刽子手游戏!”)
newPrint(“我从我的超级机密列表中随机选择了一个单词,在你的贴纸工死之前试着猜一下!”)
def newPrint(消息,两者均=1):
msg=“\n”+消息
如果(两个!=1):
msg+=“\n”
打印(msg)
def main():
全局猜测,Haswen,状态,播放时间,单词,单词列表
设置游戏()
newPrint(“我的单词是”+str(len(word))+“字母长。”)
而(wantsToPlay()==1):
单词=随机。选择(单词列表)
猜测=[]
播放时间=1
哈斯旺=0
状态=0
而(hasGuessed()==0且状态<7):
drawStickman()
drawWord()
takeNewLetter()
drawStickman()
newPrint(“我的单词是”+单词)
main()
你真正需要的是一名球员可以失误的次数。因此,我使用菜单()
返回该值:
def menu():
print("""
1. Easy (9 misses)
2. Medium (7 misses)
3. Hard (5 misses)
""")
while True:
level = input("What level do you want to play?:")
if level in ('1', '2', '3'):
return {'1': 9, '2': 7, '3': 5}[level]
print('Wrong answer!')
现在你应该把这个菜单和它的结果放到你的主程序中。在setup\u game()
之后调用菜单功能,并将contant7
替换为其结果
def main():
global guessed, hasWon, state, playedOnce, WORD, WORDLIST
setup_game()
allowed_misses = menu()
...
while (hasGuessed() == 0 and state < allowed_misses)
def main():
全局猜测,Haswen,状态,播放时间,单词,单词列表
设置游戏()
允许的\u未命中=菜单()
...
while(hasGuessed()==0,状态<允许的\u未命中)
您还应该从hasguised()
中删除状态检查,因为它是冗余的,并且使用常量值