【发布时间】:2020-03-27 17:26:13
【问题描述】:
您将如何实现元组的添加,以便对于每个不正确的答案,它将答案添加到元组并打印出来供用户检查和查看他们之前的猜测?我尝试了多种方法,但它们仅用于替换元组中的单词。
import random
words = ("blueberries", "intelligence", "reactor", "thorns", "nightmare",
"scars", "metal", "coronavirus", "industries", "heart", "workshop",
"assistant", "bots")
variable = random.choice(words)
beg = variable[:1]
end = variable[-1:]
length = len(variable)
name = input("Hello USER. Please input your name into the console.")
print("Hello " + name + ". Today, we will be playing a word guessing game. I will be giving you the first and last letter along with the length of the word. You must guess what the word is based on these clues.")
print("1st Letter: " + beg)
print("Last Letter: " + end)
print("Length of word: " + str(length))
guess = input("What is your guess?")
while variable != guess:
if guess != variable:
guess = input("Your answer is incorrect. Please try again.")
elif guess == variable:
break
print("Congratulations, you have guessed correctly.")
【问题讨论】:
-
“添加”是什么意思?您尝试过的多种事情是什么?请展示一个完整的示例,说明输入可能是什么,以及您期望的结果是什么。
-
你可以创建一个列表,然后对这个列表执行 li.append(guess)。使用 li = [] 或 li = list() 创建列表。
-
beg = variable[0]; end = variable[-1]会更简单。
标签: python tuples concatenation