Jane Doe2 Jane Doe2 - 2 months ago 11
Python Question

guessing numbers from 1 to 8

My program needs to guess the user's number (from 1 to 8) by only asking 3 questions. It prints the first two questions correctly but then when I press enter for the third question, it just prints the last input I did.
How to make all inputs (yes or no) lower case?

# Simple Expert System
#firstQuestion = prstr(firstQuestion.lower())

print("Think of a number between 1 and 8.")

firstQuestion = (raw_input("Is it an even number? "))
secondQuestion = "Is it less than or equal to 4? "
thirdQuestion = "Is it less than or equal to 3? "
fourthQuestion = "Is it less than 3? "
fifthQuestion = "Is it greater than 6? "
sixthQuestion = "Is it greater than 5? "
seventhQuestion = "Is it less than 2? "




if firstQuestion == "yes":
print(raw_input(secondQuestion))
elif firstQuestion == "no":
print(raw_input(thirdQuestion))
elif secondQuestion == "yes":
print(raw_input(fourthQuestion))
elif secondQuestion == "no":
print(raw_input(fifthQuestion))
elif thirdQuestion == "no":
print(raw_input(sixthQuestion))
elif thirdQuestion == "yes":
print(raw_input(seventhQuestion))

elif fourthQuestion == "yes":
print("Your number is 2")
elif fourthQuestion == "no":
print("Your number is 4")

elif fifthQuestion == "yes":
print("Your number is 8")
elif fifthQuestion == "no":
print("Your number is 6")

elif sixthQuestion == "yes":
print("Your number is 7")
elif sixthQuestion == "no":
print("Your number is 5")

elif seventhQuestion == "yes":
print("Your number is 1")
elif seventhQuestion == "no":
print("Your number is 3")

Answer

First you ask for the input on the first question. This puts the answer into the firstQuestion variable. Then you go into the if-section. There you ask for the raw_input for another question and then you tell the program to print that value. At that point one the elif's has been succesfull and the others are skipped.

What you should do for the desired result is to create a seperate if-group for each new question that should be asked or create a while-loop.

For example:

# Simple Expert System
#firstQuestion = prstr(firstQuestion.lower()) 

print("Think of a number between 1 and 8.")

firstQuestion = (raw_input("Is it an even number? "))
secondQuestion = "Is it less than or equal to 4? "
thirdQuestion = "Is it less than or equal to 3? "
fourthQuestion = "Is it less than 3? "
fifthQuestion = "Is it greater than 6? "
sixthQuestion = "Is it greater than 5? "
seventhQuestion = "Is it less than 2? "




if firstQuestion == "yes":
    secondQuestion = raw_input(secondQuestion)
elif firstQuestion == "no":
    thirdQuestion = raw_input(thirdQuestion)

if secondQuestion == "yes":
    fourthQuestion = raw_input(fourthQuestion)
elif secondQuestion == "no":
    fifthQuestion = raw_input(fifthQuestion)

if thirdQuestion == "no":
    sixthQuestion = raw_input(sixthQuestion)
elif thirdQuestion == "yes":
    seventhQuestion = raw_input(seventhQuestion)

if fourthQuestion == "yes":
    print("Your number is 2")
elif fourthQuestion == "no":
    print("Your number is 4")

if fifthQuestion == "yes":
    print("Your number is 8")
elif fifthQuestion == "no":
    print("Your number is 6")   

if sixthQuestion == "yes":
    print("Your number is 7")
elif sixthQuestion == "no":
    print("Your number is 5")       

if seventhQuestion == "yes":
    print("Your number is 1")
elif seventhQuestion == "no":
    print("Your number is 3")   
Comments