mzxay 发表于 2020-4-14 10:20:57

求大神解决

求大神分析一下代码的错误并改正

from tkinter import *
from tkinter import messagebox as g
import random
root = Tk()
root.title("猜数字")
root.geometry("250x150")

shu = random.randint(1, 100)

def ti_jiao():
    a_g = int(a.get())
    if a_g == shu:
      g.showinfo('正确', '恭喜你答对了')
    else:
      if a_g > 100:
            g.showinfo('输入错误','请输入1~100的整数')
      if a_g > shu and < 100 :
            g.showinfo('错误', '你猜大了')
      elif a_g < shu:
            g.showinfo('错误', '你猜小了')

Label(root, text = '1~100选一个数', bg = 'yellow', fg = 'blue',
    font = ('华文行楷', 15)).place(x = 50, y = 0)

a = Entry(root, bg = 'yellow', fg = 'blue', font = ('华文行楷', 15))
a.place(x = 20, y = 50)

b = Button(root, text='提交', bg = 'yellow', fg = 'blue', font = ('华文行楷', 15), command = ti_jiao)
b.place(x = 80, y = 90)

root.mainloop()


万分感谢

wuqramy 发表于 2020-4-14 10:27:07

本帖最后由 wuqramy 于 2020-4-14 20:46 编辑

记住,在Python中一定不能这么写:

if a_g > shu and < 100 :

得这么写:

if a_g > shu and a_g < 100 :

以下是正确代码:

from tkinter import *
from tkinter import messagebox as g
import random
root = Tk()
root.title("猜数字")
root.geometry("250x150")

shu = random.randint(1, 100)

def ti_jiao():
    a_g = int(a.get())
    if a_g == shu:
      g.showinfo('正确', '恭喜你答对了')
    else:
      if a_g > 100:
            g.showinfo('输入错误','请输入1~100的整数')
      if a_g > shu and a_g < 100 :
            g.showinfo('错误', '你猜大了')
      elif a_g < shu:
            g.showinfo('错误', '你猜小了')

Label(root, text = '1~100选一个数', bg = 'yellow', fg = 'blue',
    font = ('华文行楷', 15)).place(x = 50, y = 0)

a = Entry(root, bg = 'yellow', fg = 'blue', font = ('华文行楷', 15))
a.place(x = 20, y = 50)

b = Button(root, text='提交', bg = 'yellow', fg = 'blue', font = ('华文行楷', 15), command = ti_jiao)
b.place(x = 80, y = 90)

root.mainloop()

zltzlt 发表于 2020-4-14 13:06:53

一处地方有语法问题

from tkinter import *
from tkinter import messagebox as g
import random

root = Tk()
root.title("猜数字")
root.geometry("250x150")

shu = random.randint(1, 100)


def ti_jiao():
    a_g = int(a.get())
    if a_g == shu:
      g.showinfo('正确', '恭喜你答对了')
    else:
      if a_g > 100:
            g.showinfo('输入错误', '请输入1~100的整数')
      if shu < a_g < 100:# 此处语法有问题,改了
            g.showinfo('错误', '你猜大了')
      elif a_g < shu:
            g.showinfo('错误', '你猜小了')


Label(root, text='1~100选一个数', bg='yellow', fg='blue',
      font=('华文行楷', 15)).place(x=50, y=0)

a = Entry(root, bg='yellow', fg='blue', font=('华文行楷', 15))
a.place(x=20, y=50)

b = Button(root, text='提交', bg='yellow', fg='blue', font=('华文行楷', 15), command=ti_jiao)
b.place(x=80, y=90)

root.mainloop()

岚冰1234 发表于 2020-4-15 09:01:57

楼上的都说过了,语法写错了,建议楼主用pycharm,这种明显的语法错误,是会标记出来的,如图
页: [1]
查看完整版本: 求大神解决