Python 如何添加元组并将其打印出来?
您将如何实现元组的添加,以便对于每个不正确的答案,它都将答案添加到元组并打印出来,供用户检查和查看他们之前的猜测?我尝试了多种方法,但它们只是用来替换元组中的单词Python 如何添加元组并将其打印出来?,python,tuples,concatenation,Python,Tuples,Concatenation,您将如何实现元组的添加,以便对于每个不正确的答案,它都将答案添加到元组并打印出来,供用户检查和查看他们之前的猜测?我尝试了多种方法,但它们只是用来替换元组中的单词 import random words = ("blueberries", "intelligence", "reactor", "thorns", "nightmare", "scars", "metal", "coronavirus", "industries", "heart", "workshop"
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.")
我认为使用
列表是一个更好的选择。因为元组是不可变的。这意味着要向元组添加项,您应该创建一个新元组。但列表更具动态性,您可以更简单、更好地向列表中添加项目。
因此,您可以只使用以下代码:
previous_choices = []
# Other codes
if guess != variable:
previous_choices.append(guess)
print(previous_choices)
previous_choices = tuple()
# Other codes
if guess != variable:
previous_choices += (guess,) # In fact, every time a new tuple creates here.
print(previous_choices)
但是,如果出于某些原因希望使用元组,可以使用以下代码:
previous_choices = []
# Other codes
if guess != variable:
previous_choices.append(guess)
print(previous_choices)
previous_choices = tuple()
# Other codes
if guess != variable:
previous_choices += (guess,) # In fact, every time a new tuple creates here.
print(previous_choices)
另外,我认为最好改变你获得第一个和最后一个角色的方式。您可以简单地将变量[0]
用于beg
变量,将变量[-1]
用于end
变量。您不需要添加错误的猜测,但需要的是将它们串联起来。为此,我初始化了一个列表,然后在列表中添加了错误的猜测,因为元组是不可变的。然而,如果您需要tuple中的结果,那么最终您可以转换tuple中的列表。我对代码做了一些修改。请检查一下
words = ("blueberries", "intelligence", "reactor", "thorns", "nightmare", "scars", "metal", "coronavirus", "industries", "heart", "workshop", "assistant", "bots")
import random
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?")
wrong_guess=[]
while variable != guess:
if guess != variable:
wrong_guess.append(guess)
guess = input("Your answer is incorrect. Please try again.")
print("wrong guesses till now are:",wrong_guess)
elif guess == variable:
break
print("Congratulations, you have guessed correctly.")
“添加”是什么意思?你尝试过的多件事是什么?请给出一个完整的示例,说明输入可能是什么,以及您希望得到什么样的结果。您可以创建一个列表,并对该列表执行li.append(guess)操作。创建列表时使用:li=[]或li=list()。beg=variable[0];end=变量[-1]
会更简单。