#!/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
好的,我一直在第20行得到缩进错误。我无法弄明白。我的语法很好。我回去删除了所有的缩进并重新缩进它们(以确保混合中没有空格)。为什么给我这个?我该如何解决?
最让我困惑的是我写了相同的代码,但没有导入EasyGUI模块而没有EasyGUI修改,但除此之外它是相同的代码。 if / elif / else是在同一个地方,除了'eg.msgbox'省略'print'命令并且raw_input被'boolbox'替换之外,一切都是相同的。
编辑 - 第20行是:
if yon == 0:
线
答案 0 :(得分:1)
if yon == 0:
choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low')
你错过了一个结束括号。
choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low'))
另外,请检查您的上一个else:
。它应该是: -
else:
eg.msgbox("Enter valid input!","Lets start again from the last step")
continue
您没有正确缩进上一个if
: -
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
你的所有if-elif-else
都应该在你的外面。您正在检查内部定义的变量的条件。因此,将结构重新缩进为: -
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