如何允许代码继续从一个问题到另一个问题,即使输入答案有错误的3次尝试?



import time
import random
#declare variables and constant
guessingelement = ["Hydrogen", "Magnesium", "Cobalt", "Mercury", "Aluminium", "Uranium", "Antimony"]
nicephrases = ["Nice job", "Marvellous", "Wonderful", "Bingo", "Dynamite"]
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
guess_no = 0
score = 0
#set the maximum number of questions for looping and random pick an element from the list before deleting it
for i in range(7):
randomelement = random.choice(guessingelement)
guessingelement.remove(randomelement)
time.sleep(2)
#tips of the element
if randomelement == "Hydrogen" and not out_of_guesses:
print("Tip 1: It is the most flammable of all the known substances.")
print("Tip 2: It reacts with oxides and chlorides of many metals, like copper, lead, mercury, to produce free metals.")
print("Tip 3: It reacts with oxygen to form water.")
#test the number of tries so that it doesn't exceed 3 times if answer is wrong
while guess != randomelement and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
out_of_guesses = True
#add score, praise when answer is correct and encourage when answer is wrong for 3 times
if out_of_guesses:
print("Out of Guesses, NICE EFFORT!")
else:
print(random.choice(nicephrases), ", YOU GET IT!")
score = score + 1
#tips of the element
if randomelement == "Magnesium" and not out_of_guesses:
print("Tip 1: It has the atomic number of 12.")
print("Tip 2: It's oxide can be extracted into free metal through electrolysis.")
print("Tip 3: It is a type of metal.")

与第一个问题的代码相同。等等....




In the progress of changing:`
#tips of the element
if randomelement == "Hydrogen":
print("Tip 1: It is the most flammable of all the known substances.")
print("Tip 2: It reacts with oxides and chlorides of many metals, like copper, lead, mercury, to produce free metals.")
print("Tip 3: It reacts with oxygen to form water.")
#test the number of tries so that it doesn't exceed 3 times if answer is wrong

while guess != randomelement:
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
print(random.choice(wronganswers))
#add score, praise when answer is correct and encourage when answer is wrong for 3 times
else:
print(random.choice(nicephrases), ", YOU GET IT!")
score = score + 1

但是,在尝试3次之后,它仍然不停地打印错误答案列表中的元素,
无法进行下一题。我期望的输出是,当输入答案错误时,它将显示列表中的一个元素,并且
继续下一个问题。

希望这对你有帮助。我用一种新的方式为自己编写了这段代码。我使用递归来保持猜测的发生,并简单地使用了一个while循环,当最大尝试次数超过3时将中断。

import random
elements = ["hydrogen", "magnesium", "cobalt", "mercury", "aluminium", "uranium", "antimony"]
nice_phrases = ["Nice job", "Marvellous", "Wonderful", "Bingo", "Dynamite"]
# I went ahead and created a dictionary of lists for storing the Hints
hints = {
'hydrogen':
[
"Tip 1: It is the most flammable of all the known substances.",
"Tip 2: It reacts with oxides and chlorides of many metals, "
"like copper, lead, mercury, to produce free metals.",
"Tip 3: It reacts with oxygen to form water."
],
'magnesium':
[
"Tip 1: It has the atomic number of 12.",
"Tip 2: It's oxide can be extracted into free metal through electrolysis.",
"Tip 3: It is a type of metal."
],
}
score = 0

def guess_again():
global score
random_element = random.choice(elements)
max_attempts = 3
# this will remove the element from occurring again
for x in elements:
if x == random_element:
elements.remove(x)
while max_attempts > 0:
user_guess = input("Take a Guess").lower()
if user_guess == random_element:
print(f"{random.choice(nice_phrases)}, you got it!")
score += 1
# If the answer is right calling the function again will continue the game
guess_again()
else:
max_attempts -= 1
print("That was a wrong guess. Here is a Hint")
if random_element in hints:
print(hints[random_element])
else:
print("Sorry no hints available at the moment")
if max_attempts == 0:
print("Sorry your out of guesses")
print(f"{random.choice} was the element")

guess_again()

如果答案是正确的,它将选择下一个元素,游戏继续,直到列表中的所有元素都完成。我已经编码它给每个元素3次尝试。如果你想在整个游戏过程中最多尝试3次只需在函数外声明max_attempts并赋予其全局作用域,就像score

一样

最新更新