I am trying to code this question and system that would let you ask multiple questions as you get them right or wrong until you have no strikes left. At first, I wrote
question = "Name something a baby does that would be unacceptable in a roommate."
possible_answers = [("CRY/AT 3 A.M.", 39), ("POO/WET SELF", 28), ("PUKE/ON ME", 9), ("BURP", 8), ("MAKE MESS/TOSS FOOD", 5), ("FART", 4), ("PEE IN MY FACE", 3), ("NURSE/ON MY NIPS", 2)]
player_strikes_left = 3
player_score = 0
player_answer = input("player answer: ")
player_answer = player_answer.upper()
while player_strikes_left > 0:
for item in possible_answers:
points = item[1]
is_correct = (player_answer in answer)
if is_correct:
print("true")
player_score = player_score points
else:
player_strikes_left -= 1
print(f'player score: {player_score}, player strikes left: {player_strikes_left}')
But Because of the 'for' loop, it is searching for the answer in every possible_answer and reducing the point for every wrong one so I changed it to this:
question = "Name something a baby does that would be unacceptable in a roommate."
possible_answers = [("CRY/AT 3 A.M.", 39), ("POO/WET SELF", 28), ("PUKE/ON ME", 9), ("BURP", 8), ("MAKE MESS/TOSS FOOD", 5), ("FART", 4), ("PEE IN MY FACE", 3), ("NURSE/ON MY NIPS", 2)]
player_strikes_left = 3
player_score = 0
while player_strikes_left > 0:
player_answer = input("player answer: ")
player_answer = player_answer.upper()
item = possible_answers
answer = item[0]
points = item[1]
is_correct = (player_answer in answer)
if is_correct:
player_score = player_score points
else:
player_strikes_left -= 1
while player_strikes_left == 0:
break
but now it is not adding up the points. I can write my answers 3 times until player_strikes_left hits 0 and the code ends after that since it is not accepting any of my answers as correct. I'm guessing it might be because of how points and the answers part of the code is messed up since there is no for loop there anymore. Does anyone have any idea what I can do and how I can fix this problem? Thank you beforehand.
CodePudding user response:
Because answer = item[0]
and item = possible_answers
, the value of answer
is ("CRY/AT 3 A.M.", 39)
, so you end up only checking if the player answer is equal to "CRY/AT 3 A.M." or 39.
A better way might be to store possible answers and their scores as a dictionary like possible_answers = {"CRY/AT 3 A.M.": 39, ...)
. Then is_correct = player_answer in possible_answers
and score = possible_answers[player_answer]
. You can also use the .get()
method of dict which you can give a default value if there is no match.
https://docs.python.org/3/library/stdtypes.html#dict
CodePudding user response:
You don't increase score because you do
item = possible_answers
answer = item[0]
where you change item into the list of possible answers, and then select the first element of this (("CRY/AT 3 A.M.", 39)
).
You would want to do something like
question = "Name something a baby does that would be unacceptable in a roommate."
possible_answers = [("CRY/AT 3 A.M.", 39), ("POO/WET SELF", 28), ("PUKE/ON ME", 9), ("BURP", 8), ("MAKE MESS/TOSS FOOD", 5), ("FART", 4), ("PEE IN MY FACE", 3), ("NURSE/ON MY NIPS", 2)]
player_strikes_left = 3
player_score = 0
player_answer = input("player answer: ")
player_answer = player_answer.upper()
while player_strikes_left > 0:
none_correct = True
for item in possible_answers:
points = item[1]
is_correct = (player_answer in answer)
if is_correct:
print("true")
player_score = player_score points
none_correct = False
if none_correct:
player_strikes_left -= 1
print(f'player score: {player_score}, player strikes left: {player_strikes_left}')
CodePudding user response:
def main():
print("Name something a baby does that would be unacceptable in a roommate.")
possible_answers = [("CRY/AT 3 A.M.", 39), ("POO/WET SELF", 28), ("PUKE/ON ME", 9), ("BURP", 8),
("MAKE MESS/TOSS FOOD", 5), ("FART", 4), ("PEE IN MY FACE", 3), ("NURSE/ON MY NIPS", 2)]
player_strikes_left = 3
player_score = 0
match = False
while player_strikes_left > 0:
player_answer = input("player answer: ")
player_answer = player_answer.upper()
for item in possible_answers:
answer = item[0]
if player_answer in answer:
player_score = item[1]
match = True
break
if not match:
player_strikes_left -= 1
match = False
print(player_score)
if __name__ == "__main__":
main()
This script does what seems to be the intention of your sample code. It checks if it can find the player answer in one of the example answers, however the words do not have to match and the program also increases the score when a single letter is entered that is also present in one of the example answers.