[英]How do I prompt a user to key in correct input and reuse that input to go through a loop?
我对我的一项作业有疑问。
在文件“scrabble.py”中,创建一个程序:
我的解决方案:
字母 = ['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l' ,'m','n','o','p','q','r','s','t','u','v','w','x',' y', 'z'] 点 = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1、4、4、8、4、10]
新 = [ ]
word = input("输入一个单词(小写):")
对于榆树来说:
if elm in letters:
idx = letters.index(elm)
new.append(points[idx])
continue
if elm not in letters:
print("Key in a proper word!")
word = input("type a word (in lower case): ")
打印(新)
Sum_of_points = 总和(新)
print(f"总分是:{Sum_of_points}")
我面临的问题是,用户一开始输入错误的数字后,它会再次提示用户,但是它不会从一开始就重复循环。 我希望它重复循环,我应该如何 go 关于这样做? 我也将非常感谢有关如何改进这一点的反馈!
您可以尝试使用 dict 而不是两个列表,以便将字母和与字母关联的点绑定在一起。 这样,您可以避免任何字符串方法。
scrabble_dict = {
'a' : 1,
'b' : 3,
'c' : 3
}
word = input("type a word (in lower case): ")
new = []
for elm in word:
if elm in scrabble_dict.keys():
new.append(scrabble_dict[elm])
else:
print("Key in a proper word!")
word = input("type a word (in lower case): ")
回答您的编辑(...在开始时输入错误的数字,它会再次提示用户,但是它不会从一开始就重复循环。我希望它重复循环...)
你可以 -
input_remaining = True
while input_remaining:
word = input("type a word (in lower case): ")
new = []
for elm in word:
if elm in scrabble_dict.keys():
new.append(scrabble_dict[elm])
else:
print("Key in a proper word!")
break
input_remaining = False
或者
word = input("type a word (in lower case): ")
new = []
for elm in word:
if elm in scrabble_dict.keys():
new.append(scrabble_dict[elm])
else:
print("letter appears invalid")
elm = input("type in correct letter ")
if elm in scrabble_dict.keys():
new.append(scrabble_dict[elm])
else:
print("wrong again, I quit!)
exit()
在第二种方式中,用户不必再次重复整个单词。 您可能可以创建一个 function 来检查字母是否有效,然后在尝试 3 次后exit()
这将不断提示用户输入一个新的字符串,直到给出一个有效的单词。 此外,您可以使用 ASCII 字符减法来避免使用诸如“索引”之类的字符串方法。 最后,我们不需要一个完整的“字母”数组,我们只需检查字符是否在 'a' 到 'z' 的范围内
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
while word := input("type a word (in lower case): "):
valid_word = True
new = []
print(word)
for elm in word:
if elm >= 'a' and elm <= 'z':
new.append(points[ord(elm) - ord('a')])
continue
else:
valid_word = False
print("Key in a proper word!")
break
Sum_of_points = sum(new)
if valid_word:
break
print(f"The total points is: {Sum_of_points}")
在进入 for 循环之前,您需要确保第一个字母在字母中,方法是添加一个 while 循环:
new = []
while True:
word = input("type a word (in lower case): ")
if (word[0] in letters):
break;
for elm in word:
if elm in letters:
idx = letters.index(elm)
new.append(points[idx])
continue
if elm not in letters:
print("Key in a proper word!")
word = input("type a word (in lower case): ")
print(new)
Sum_of_points = sum(new)
print(f"The total points is: {Sum_of_points}")
这是一个带有注释的示例代码:)(用 Python3 编写)
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
# Easier look up via dictionary
mapping = {k:v for k, v in zip(letters, points)}
# Checking from time to time with while loop
_input = input("type a word (in lower case): ")
while set(_input) - set(letters):
print("Key in a proper word!")
_input = input("type a word (in lower case): ")
# Calculate the summation through dict look up
sum_of_points = sum(map(mapping.__getitem__, _input))
print(f"The total points is: {sum_of_points}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.