【问题标题】:If statement conditions being ignored and going straight to else no matter what you inputIf 语句条件被忽略并直接进入 else 无论您输入什么
【发布时间】:2020-07-21 03:24:04
【问题描述】:

我对 Python 有点陌生,我正在为一个项目创建一个测试程序,这是一个你必须猜测某些 Python 短语的定义的游戏。前半部分工作正常,后半部分有问题。它应该从元组中随机选择一个定义,并要求您输入正确的单词。但是,无论它是什么词,它总是会转到您输入错误答案的 else 语句。我尝试了很多东西,改变了括号、变量以及介于两者之间的所有内容。 这是有问题的下半场: (变量定义在顶部,不包括在内)

while (len(wordPool) >= 1):
  answer = input(random.choice(question) + " What word is that? ")
  if question == vocabularyDefinitions[0] and answer == "List":
        print("You got it right! That is a list!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[1]  and answer == "Tuple":
        print("That is correct! That is a tuple!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[2] and answer == "Datetime":
        print("Nice job! That is the datetime module!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[3] and answer == "Random":
        print("That's right! That is in fact the random module!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[4] and answer == "Include":
        print("Yep, that's the include function! Well done!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[5] and answer == "Parameter":
        print("That is a parameter, keep it up!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[6] and answer == "Index":
        print("You must know your stuff! That's an index alright.")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[7] and answer == "Len":
        print("This congratulatory message says well done!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[8] and answer == "Append":
        print("You have a knack at this don't you? Keep going!")
        score +=1
        totalAttempts += 1
  elif question == vocabularyDefinitions[9] and answer == "Insert":
        print("Yes! You answered correctly!")
        score += 1
        totalAttempts += 1 
  elif question == vocabularyDefinitions[10] and answer == "Remove":
        print("I definitely won't remove you after that correct answer! Which was remove by the way.")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[11] and answer == "Iterate":
        print("That was in fact the definition of iterate in Python!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[12] and answer == "Loop":
        print("It's highly unlikely the while loop will show you this message again, but if it does, consider yourself lucky!")
        score += 1
        totalAttempts += 1
  elif question == vocabularyDefinitions[13] and answer == "Element":
        print("The index number of this word is the same as the atomic number for Aluminum. The more you know!")
        score += 1
        totalAttempts += 1
  elif (answer == "End"):
        break
  else:
        print("Whoops! That wasn't the right answer!")
        wrongAnswer += 1

print(str.format("The game is over, and got {} questions right, {} questions wrong, and your total attempts was {}.",score,wrongAnswer,totalAttempts))

我可以添加/删除什么以便在您的输入中不忽略 if/elif 语句?

【问题讨论】:

  • 绝对确定question 直接来自vocabularyDefinitions?可能略有不同,因此无法正确比较。
  • 我通过简单地说它等于词汇定义来使问题变量。我不想在 if/elif 中使用后一个变量,因为使用等于自身的相同变量会很奇怪。
  • 尝试在while 循环的开始处分配question = random.choice(vocabularyDefinitions)。这样,您可以将其视为“选择的问题”,然后将其与它的来源进行比较。
  • 奇怪的是,当我这样做并进行随机化时,定义甚至没有出现,只有输入的字符串部分出现。这与我之前做过的类似策略很熟悉,唯一的区别是我将问题分配放在 while 循环之上,而当我这样做时,定义只是“问题”一词的一个字母。
  • 请说明您如何分配vocabulary Definitionsquestion。这就是问题所在,所以除非您展示出来,否则我们无法帮助您。

标签: python


【解决方案1】:

据我推测,由于该变量未包含在帖子中,因此“问题”是可迭代的。 (所以为了简单起见,假设问题是一个列表)

所以你做了...

answer = input(random.choice(question) + " What word is that? ")

为什么要比较列表和字符串?

question == vocabularyDefinitions[0]

【讨论】:

  • 这似乎是一个问题,而不是一个答案。
  • @Barmar 我的错。我试图向 OP 指出,比较可能是在 2 种不同的数据类型之间。因此,从来没有达到 if/elif 条件的原因。
  • 这真的是评论,而不是答案。多一点代表,you will be able to post comments.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多