ComputerExpert69 ComputerExpert69 - 1 month ago 7
Python Question

Python - Guess Who game - Im not getting my expected output

This is a small portion of my guess who game im developing that assigns the AI Opponents randomly chosen character with its linked features, and assigns those features to the feature variables. When the user asks the question the if statements will respond with a yes or no based on whether it has the features asked off, but i am not getting an expected output. I may not be able to reply with any further questions about this until i get back from college later today thanks.

I do not get any errors, but every response is a "No" even when the AI opponent 100% definetely has that feature. Heres the output im getting:


What is your question for your AI opponent?
Does your character have short hair?
AI Opponent: No
What is your question for your AI opponent?
Does your character have long hair?
AI Opponent: No
What is your question for your AI opponent?
Is your character Bald?
AI Opponent: No
What is your question for your AI opponent?
Is your character male?
AI Opponent: No
What is your question for your AI opponent?
Is your character female?
AI Opponent: No


#This assigns a random character name to the variable 'AICharacterChoice'
AICharacterChoice = random.choice(["Greg", "Chris", "Jason", "Clancy", "Betty", "Selena", "Helen", "Jacqueline"])
#This simpy defines these feature variables
AIChoiceFeatureHairLength = "Unassigned"
AIChoiceFeatureHairColour = "Unassigned"
AIChoiceFeatureFacialHair = "Unassigned"
AIChoiceFeatureJewellery = "Unassigned"
AIChoiceFeatureHat = "Unassigned"
AIChoiceFeatureLipstick = "Unassigned"
AIChoiceGender = "Unassigned"
#This assigns the feature variables with features linked to that characters name
if AICharacterChoice == "Greg":
AIChoiceFeatureHairLength == "Short"
AIChoiceFeatureHairColour == "Brown"
AIChoiceFeatureFacialHair == "Yes"
AIChoiceFeatureJewellery == "Yes"
AIChoiceFeatureHat == "No"
AIChoiceFeatureLipstick == "No"
AIChoiceGender == "Male"

if AICharacterChoice == "Chris":
AIChoiceFeatureHairLength == "Long"
AIChoiceFeatureHairColour == "Blonde"
AIChoiceFeatureFacialHair == "No"
AIChoiceFeatureJewellery == "No"
AIChoiceFeatureHat == "Yes"
AIChoiceFeatureLipstick == "Yes"
AIChoiceGender == "Male"

if AICharacterChoice == "Jason":
AIChoiceFeatureHairLength == "Short"
AIChoiceFeatureHairColour == "Brown"
AIChoiceFeatureFacialHair == "Yes"
AIChoiceFeatureJewellery == "No"
AIChoiceFeatureHat == "Yes"
AIChoiceFeatureLipstick == "No"
AIChoiceGender == "Male"

if AICharacterChoice == "Clancy":
AIChoiceFeatureHairLength == "Bald"
AIChoiceFeatureHairColour == "Red"
AIChoiceFeatureFacialHair == "Yes"
AIChoiceFeatureJewellery == "No"
AIChoiceFeatureHat == "No"
AIChoiceFeatureLipstick == "No"
AIChoiceGender == "Male"

if AICharacterChoice == "Betty":
AIChoiceFeatureHairLength == "Bald"
AIChoiceFeatureHairColour == "Blonde"
AIChoiceFeatureFacialHair == "No"
AIChoiceFeatureJewellery == "Yes"
AIChoiceFeatureHat == "Yes"
AIChoiceFeatureLipstick == "Yes"
AIChoiceGender == "Female"

if AICharacterChoice == "Selena":
AIChoiceFeatureHairLength == "Long"
AIChoiceFeatureHairColour == "Brown"
AIChoiceFeatureFacialHair == "No"
AIChoiceFeatureJewellery == "Yes"
AIChoiceFeatureHat == "No"
AIChoiceFeatureLipstick == "No"
AIChoiceGender == "Female"

if AICharacterChoice == "Helen":
AIChoiceFeatureHairLength == "Short"
AIChoiceFeatureHairColour == "Brown"
AIChoiceFeatureFacialHair == "No"
AIChoiceFeatureJewellery == "No"
AIChoiceFeatureHat == "No"
AIChoiceFeatureLipstick == "Yes"
AIChoiceGender == "Female"

if AICharacterChoice == "Jacqueline":
AIChoiceFeatureHairLength == "Long"
AIChoiceFeatureHairColour == "Red"
AIChoiceFeatureFacialHair == "Yes"
AIChoiceFeatureJewellery == "Yes"
AIChoiceFeatureHat == "No"
AIChoiceFeatureLipstick == "No"
AIChoiceGender == "Female"
#This loops the questions to ask the AI opponent
x = 1
while x == 1:
#This asks the user what question they would like to ask the AI opponent, when they ask the question the if statements will reply with a "yes" or "no" based on whether is has that feature
QuestionForAI = input("What is your question for your AI opponent? ").upper()
if QuestionForAI == "DOES YOUR CHARACTER HAVE SHORT HAIR?" and "DOES YOUR CHARACTER HAVE SHORT HAIR":
if AIChoiceFeatureHairLength == "Short":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")
if QuestionForAI == "DOES YOUR CHARACTER HAVE LONG HAIR?" and "DOES YOUR CHARACTER HAVE LONG HAIR":
if AIChoiceFeatureHairLength == "Long":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")
if QuestionForAI == "DOES YOUR CHARACTER HAVE FACIAL HAIR?" and "DOES YOUR CHARACTER HAVE FACIAL HAIR":
if AIChoiceFeatureHairColour == "Yes":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")
if QuestionForAI == "IS YOUR CHARACTER MALE?" and "IS YOUR CHARACTER MALE":
if AIChoiceGender == "Male":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "IS YOUR CHARACTER FEMALE?" and "IS YOUR CHARACTER FEMALE":
if AIChoiceGender == "Female":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "DOES YOUR CHARACTER WEAR A HAT?" and "DOES YOUR CHARACTER WEAR A HAT":
if AIChoiceFeatureHat == "Yes":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "DOES YOUR CHARACTER WEAR LIPSTICK?" and "DOES YOUR CHARACTER WEAR LIPSTICK":
if AIChoiceFeatureLipstick == "Yes":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "DOES YOUR CHARACTER WEAR JEWELLERY?" and "DOESYOURCHARACTERWEARJEWELLERY?":
if AIChoiceFeatureJewellery == "Yes":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "IS YOUR CHARACTER BLONDE HAIRED?" and "IS YOUR CHARACTER BLONDE HAIRED":
if AIChoiceFeatureHairColour == "Blonde":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "IS YOUR CHARACTER BROWN HAIRED?" and "IS YOUR CHARACTER BROWN HAIRED":
if AIChoiceFeatureHairColour == "Brown":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "IS YOUR CHARACTER BALD?" and "ISYOURCHARACTERBALD?":
if AIChoiceFeatureHairLength == "Bald":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

if QuestionForAI == "IS YOUR CHARACTER RED HAIRED?" and "IS YOUR CHARACTER RED HAIRED":
if AIChoiceFeatureHairColour == "Red":
print("AI Opponent: Yes")
else:
print("AI Opponent: No")

Answer

AIChoiceFeatureHairLength == "Short" is a conditional statement, it will evaluate to either Trueor False.

What you want to do is a assignement like this: AIChoiceFeatureHairLength = "Short"

I would also recomment using dictionaries like:

Greg = {"HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"Yes", "FeatureHat":"No", "Lipstick":"No","Gender":"Male"}`

You can put the dictionaries in a list ([Greg, Chris, etc]) and use the random.choice() function on that.

You can acces an entry in a dictionary with Greg["HairLenght"] and it will return "Short".

This will shorten the code a lot and you also don't need to introduce the variables as ="unassigned"

Comments