Question

#!/usr/bin/env python
import easygui as eg


start = eg.msgbox('In your head, pick a number between 1 and 100. No matter what, I will guess it in no more than 7 tries. When you think of a number, press OK to start the program.')

maximum = 100
minimum = 0
middle = (maximum-minimum)/2
attempt= 1
while True:
    if attempt > 7:
        eg.msgbox('You cheated. Terminating...')
        break
    else:   
        yon = eg.boolbox("Is your number: " + str(middle) + '?', 'Guess Result',('Yes','No'))
        if yon == 1:
            eg.msgbox("Found in " + str(attempt) + " try!")
            break
        if yon == 0:
            choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low')
        if choice == 0:
            minimum = middle
            middle = int(round(float(maximum+minimum)/2))
        elif choice == 1:
            maximum = middle
            middle = int(round(float(maximum+minimum)/2))
        else:
            eg.msgbox"Enter valid input!";"Lets start again from the last step"
            continue
        attempt+= 1

Okay, I keep getting an indent error on Line 20. I can't figure it out. My syntax appears fine. I went back and deleted all the indents and re-indented them(to make sure there were no spaces in the mix). Why is it giving me this? How can I fix it?

What really confuses me the most is I have the same code written out, but without the EasyGUI module imported and without the EasyGUI modifications, but it's the same code other than that. The if/elif/else's are in the same spot, and everything is the same except the 'print' commands have been omitted for 'eg.msgbox' and the raw_input's have been replaced with a 'boolbox'.

EDIT - Line 20 is the:

 if yon == 0:

line

Was it helpful?

Solution

if yon == 0:
    choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low')

You have missed one closing bracket.

choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low'))

Also, check your last else:. It should be: -

else:
    eg.msgbox("Enter valid input!","Lets start again from the last step")
    continue

You have not indented your last if properly:-

    if yon == 0:
        choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low')
    if choice == 0:
        minimum = middle
        middle = int(round(float(maximum+minimum)/2))
    elif choice == 1:
        maximum = middle
        middle = int(round(float(maximum+minimum)/2))
    else:
        eg.msgbox"Enter valid input!";"Lets start again from the last step"
        continue

All your if-elif-else should be inside your outer if. You are checking condition on variable defined inside that.. So re-indent your structure to: -

if yon == 0:
    choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low')
    if choice == 0:
        minimum = middle
        middle = int(round(float(maximum+minimum)/2))
    elif choice == 1:
        maximum = middle
        middle = int(round(float(maximum+minimum)/2))
    else:
        eg.msgbox"Enter valid input!";"Lets start again from the last step"
        continue
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top