我试图让print语句运行,而不必在每次“骨骼”迭代运行时都运行。应该在做出两个猜测之后。
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
def foot_bones_quiz(guess, answer):
total_bones = 0
for bones in answer:
total_bones += bones.count(bones)
if guess.lower() == bones.lower():
return True
else:
pass
return False
**print("Total number of identified bones: ", total_bones)**
guess = 0
while guess < 2:
guess = guess + 1
user_guess = input("Enter a bone: ")
print("Is ", user_guess.lower(), " a foot bone?", foot_bones_quiz(user_guess, foot_bones))
print("Bye, Thanks for your answers.")
答案 0 :(得分:1)
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
# Declare total as global variable rather than in the loop, as we are calling this loop twice, and this will not store the count from previous loop iteration
total_bones = 0
def foot_bones_quiz(guess, answer):
global total_bones
for bones in answer:
# First bones is a string, so bones.count(bones) is just giving 1 all the time, so you have to increase the count, only when a bone is actually identified
if guess.lower() == bones.lower():
total_bones += bones.count(bones)
return True
else:
pass
return False
guess = 0
while guess < 2:
guess = guess + 1
user_guess = input("Enter a bone: ")
print("Is ", user_guess.lower(), " a foot bone?", foot_bones_quiz(user_guess, foot_bones))
print("Bye, Thanks for your answers.")
# Now we actually print, how many guesses were correct out of the 2 made
print("Total number of identified bones: ", total_bones)
在Ubuntu 3.6和python 3.6上进行了测试,并附带了屏幕截图。