Jane Doe2
Jane Doe2

Reputation: 141

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")   

Upvotes: 0

Views: 237

Answers (5)

Vivian
Vivian

Reputation: 1639

What you've got there doesn't actually use the input from any of the questions except the first. For example:

secondQuestion = "Is it less than or equal to 4? "
# other stuff
elif secondQuestion == "yes": # will never be true

Calling raw_input(secondQuestion) doesn't change the value of secondQuestion, it just returns the input. Which in your case means printing it a second time.

Also, your elifs mean that it will only check until the first one comes out true - so if you answered 'yes' to the first question, it prints and requests input for the second question, and stops there.

Something that would be more like what you want:

questions = []
questions.append("Is it an even number? ")
questions.append("Is it less than or equal to 4? ")
questions.append("Is it less than or equal to 3? ")
questions.append("Is it less than 3? ")
questions.append("Is it greater than 6? ")
questions.append("Is it greater than 5? ")
questions.append("Is it less than 2? ")


answers = [raw_input(q) for q in questions]
# Make sure to validate input
while True:
    if answers[0] == 'yes':
        is_even = True
        break
    elif answers[0] == 'no':
        is_even = False
        break
    else:
        answers[0] = raw_input("Yes or no, please. ")

# Now repeat that pattern with later questions

while True:
    if answers[1] == 'yes':
        maximum = 4
        break
    elif answers[1] == 'no':
        minimum = 5
        break
    else:
        answers[1] = raw_input("Yes or no, please. ")

# Continue collecting data like this for all questions,
# Then use it at the end to get an answer

There's definitely a more efficient way to do it - no reason to hardcode all the questions, for example, it's just a binary search - but this is closer to what you had written than that.

Upvotes: 0

acdr
acdr

Reputation: 4726

Consider that your program doesn't scale well at all for larger numbers: if you have to guess a number between 1 and 1000, you're going to have to write a lot of code.

Instead, consider looping through all the ranges that you might get:

lower_limit = 1
upper_limit = 100

while lower_limit < upper_limit:
    middle = int(0.5 * (lower_limit + upper_limit))
    check = raw_input("Larger than " + str(middle) + "? ")
    if check.lower().startswith("y"): # Accept anything that starts with a Y as "yes"
        lower_limit = middle + 1
    else:
        upper_limit = middle

print(lower_limit)

Upvotes: 6

Samuel Brush
Samuel Brush

Reputation: 21

Can you provide the input you used?

It isn't needed though, as I think improving the structure of this program would help answer your question.

For instance, notice how you write:

firstQuestion = (raw_input("Is it an even number? "))
secondQuestion = "Is it less than or equal to 4? "

Since you assigned the "raw_input..." to "firstQuestion", "firstQuestion" no longer holds the question but the answer to the question. There is a lack of consistency between these lines. It would make more sense to do something like:

firstQuestion = "Is it an even number? "
secondQuestion = "Is it less than or equal to 4? "
#put your other questions here

firstAnswer = raw_input(firstQuestion)
if firstAnswer == "yes":
...

By changing your program in a way such as this, the logic and resulting behavior will be more clear.

Upvotes: 0

Michel Touw
Michel Touw

Reputation: 626

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")   

Upvotes: 0

JMat
JMat

Reputation: 737

Indeed your program can't go further than the second question,

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

whether I answer yes or no to the first question, the code will go in one of those two cases, and therefore can't go to the rest of your program.

You have to write your program thinking of all the possible scenarios and how to reach them:

if firstQuestion == "yes":
    #The user answered "yes" to the first question
    if secondQuestion == "yes":
        #The user answered "yes" to the first question and "yes" to the second
    elif secondQuestion == "no":
        #The user answered "yes" to the first question and "no" to the second
elif firstQuestion == "no":
    #The user answered "no" to the first question
    #etc...

by continuing this graph to all levels, you have all the scenarios of your game covered

Upvotes: 0

Related Questions