【问题标题】:How to add tuples and print them out? [duplicate]如何添加元组并打印出来? [复制]
【发布时间】: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


【解决方案1】:

我认为使用list 是更好的选择。因为元组是不可变的。这意味着要将项目添加到元组,您应该创建一个新元组。但列表更具动态性,您可以更简单、更好地向列表中添加项目。 所以你可以使用下面的代码:

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)

另外,我认为最好改变获取第一个和最后一个字符的方式。您可以简单地将variable[0] 用于您的beg 变量,并将variable[-1] 用于您的end 变量。

【讨论】:

    【解决方案2】:

    您不需要添加错误的猜测,但您需要将它们连接起来。为此,我初始化了一个列表,然后在该列表中附加了错误的猜测,因为元组是不可变的。但是,如果您需要元组中的结果,那么最后您可以将列表转换为元组。我对代码做了一些更改。请检查一下。

    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.")
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 2013-04-04
      • 2021-03-16
      • 2014-03-05
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多