Python猜字游戏
目前,我和我的队友正在做一个小练习,叫猜字游戏。但是,当我们运行这个函数时。它没有显示符合教师期望的正确输出。我们也被def检查函卡住了。所以,如果我做错了什么,你们能帮我检查一下吗:Python猜字游戏,python,string,Python,String,目前,我和我的队友正在做一个小练习,叫猜字游戏。但是,当我们运行这个函数时。它没有显示符合教师期望的正确输出。我们也被def检查函卡住了。所以,如果我做错了什么,你们能帮我检查一下吗: import random def read_word_list(word_list): str_list = [] for line in word_list: for s in line.split(): try: x=
import random
def read_word_list(word_list):
str_list = []
for line in word_list:
for s in line.split():
try:
x= str(s)
str_list.append(x)
except ValueError:
pass
return str_list
words = ["introduction", "programming", "database", "concepts", "techniques", "security"]
my_list = read_word_list(words)
random_word = random.choice(my_list)
blanks = '_ ' * len(random_word)
print()
print ("Word: ",blanks)
def greetings():
name = input("What is your name? ")
print("Welcome,", name + "!")
return name
def read_char(prompt, first, last):
x= input(prompt)
while x < first or x > last:
print("Not in range. Try Again!!!")
x= input(prompt)
return x
def check_letters(target, letters):
print(len(random_word))
count = 0
for c in random_word:
if "a" <= c <= "z":
count += 1
print(count)
count2 = 0
for i in range(len(random_word)):
if "a" <= random_word[i] <= "z":
count2 += 1
print(count2)
new_s = " "
for c in random_word:
if "a" <= c <= "z" or "A" <= c <= "Z":
new_s += c
else:
new_s += "_"
print(new_s)
实际产量:
Word: _ _ _ _ _ _ _ _ _ _ _ _
Guess a word game.
What is your name? David
Welcome, David!
Please make a guess: a
guess is wrong! 9 more failed attempts allowed.
guess is wrong! 8 more failed attempts allowed.
guess is wrong! 7 more failed attempts allowed.
guess is wrong! 6 more failed attempts allowed.
guess is wrong! 5 more failed attempts allowed.
guess is wrong! 4 more failed attempts allowed.
guess is wrong! 3 more failed attempts allowed.
guess is wrong! 2 more failed attempts allowed.
guess is wrong! 1 more failed attempts allowed.
guess is wrong! 0 more failed attempts allowed.
guess is wrong! -1 more failed attempts allowed.
guess is wrong! -2 more failed attempts allowed.
Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/Exercise 4/testing.py", line 106, in <module>
print("Words: ", newBlanks)
NameError: name 'newBlanks' is not defined
Word:\uuuuuuuuuuuuuuuuuuuuuuuuuu
猜猜单词游戏。
你的名字叫什么?大卫
欢迎你,大卫!
请猜一猜:a
猜错了!允许再进行9次失败的尝试。
猜错了!允许8次以上失败的尝试。
猜错了!允许再进行7次失败的尝试。
猜错了!允许再尝试6次失败。
猜错了!允许再进行5次失败的尝试。
猜错了!允许再进行4次失败的尝试。
猜错了!允许再进行3次失败的尝试。
猜错了!允许再进行2次失败的尝试。
猜错了!允许1次以上失败的尝试。
猜错了!允许再进行0次失败的尝试。
猜错了-允许1次以上失败的尝试。
猜错了-允许再进行2次失败的尝试。
回溯(最近一次呼叫最后一次):
文件“C:/Users/USER/PycharmProjects/Exercise 4/testing.py”,第106行,在
打印(“文字:”,新行)
NameError:未定义名称“newBlanks”
您的程序中有很多错误。把它们都列在这里是不切实际的。我建议和你的老师谈谈,了解你到底做错了什么,以及如何满足他的期望。另外,你和你的朋友应该看看,以便更好地了解如何编写惯用Python代码。。。我们再也没有时间见面并征求老师的意见了。此外,我们遵循了他指导我们的相同结构。然而,关键的一点是,如果在def检查函中,我们仍然不知道如何执行它
Word: _ _ _ _ _ _ _ _ _ _ _ _
Guess a word game.
What is your name? David
Welcome, David!
Please make a guess: a
guess is wrong! 9 more failed attempts allowed.
guess is wrong! 8 more failed attempts allowed.
guess is wrong! 7 more failed attempts allowed.
guess is wrong! 6 more failed attempts allowed.
guess is wrong! 5 more failed attempts allowed.
guess is wrong! 4 more failed attempts allowed.
guess is wrong! 3 more failed attempts allowed.
guess is wrong! 2 more failed attempts allowed.
guess is wrong! 1 more failed attempts allowed.
guess is wrong! 0 more failed attempts allowed.
guess is wrong! -1 more failed attempts allowed.
guess is wrong! -2 more failed attempts allowed.
Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/Exercise 4/testing.py", line 106, in <module>
print("Words: ", newBlanks)
NameError: name 'newBlanks' is not defined