wyz601 发表于 2020-4-13 22:11:50

怎样解决这个错误

import easygui
import random
secret=random.randint(1,10)
sore=3
print ('------------I love you !-----------')
def func1(guess):
    if (guess==secret):
      easygui.msgbox ('我操,居然猜对')
      easygui.msgbox ('不过猜对也没有奖,哈哈')
      
    else:
      if(guess>secret):
            easygui.msgbox ("哥,大了大了~~!")
      else:
            easygui.msgbox("嘿,小了小了~~!!")

def func2():
    temp=easygui.enterbox('请录入你猜到的数字:')
    while temp.isdigit()!=1:
      temp=easygui.enterbox ('请输入整数数字:')
    guess=int(temp)
    func1(temp)

            
while sore>1:
    func2()
    sore-=1
   
easygui.msgbox('游戏结束!')

隔壁繁星吖 发表于 2020-4-13 22:14:19

错误呢

wyz601 发表于 2020-4-13 22:15:49

隔壁繁星吖 发表于 2020-4-13 22:14
错误呢

Traceback (most recent call last):
File "E:/A/python/Exercise/easygui 4-13.py", line 26, in <module>
    func2()
File "E:/A/python/Exercise/easygui 4-13.py", line 22, in func2
    func1(temp)
File "E:/A/python/Exercise/easygui 4-13.py", line 12, in func1
    if(guess>secret):
TypeError: '>' not supported between instances of 'str' and 'int'

admintest166 发表于 2020-4-13 22:20:48

import easygui
import random

secret = random.randint(1, 10)
sore = 3
print('------------I love you !-----------')


def func1(guess):
    if (guess == secret):
      easygui.msgbox('我操,居然猜对')
      easygui.msgbox('不过猜对也没有奖,哈哈')

    else:
      if (guess > secret):
            easygui.msgbox("哥,大了大了~~!")
      else:
            easygui.msgbox("嘿,小了小了~~!!")


def func2():
    temp = easygui.enterbox('请录入你猜到的数字:')
    while temp.isdigit() != 1:
      temp = easygui.enterbox('请输入整数数字:')
    guess = int(temp)
    func1(int(temp))


while sore > 1:
    func2()
    sore -= 1

easygui.msgbox('游戏结束!')

admintest166 发表于 2020-4-13 22:22:13

你这个问题很简单secret = random.randint(1, 10)中是一个int类型的 而你传给他的guess是一个字符串 str和int不能进行判断 类型都不一样

你原本是这样的func1(temp)这个传进去是一个字符串 我改成了func1(int(temp)) 就可以了

wyz601 发表于 2020-4-13 22:30:07

import easygui,os
import random
secret=random.randint(1,1)
sore=3
print ('------------I love you !-----------')
def func1(guess):
    if (guess==secret):
      easygui.msgbox ('我操,居然猜对')
      easygui.msgbox ('不过猜对也没有奖,哈哈')
      easygui.msgbox('游戏结束!')
      os._exit(0)
            
    else:
      if(guess>secret):
            easygui.msgbox ("哥,大了大了~~!")
      else:
            easygui.msgbox("嘿,小了小了~~!!")

def func2():
    temp=easygui.enterbox('请录入你猜到的数字:')
    while temp.isdigit()!=1:
      temp=easygui.enterbox ('请输入整数数字:')
    guess=int(temp)
    func1(guess)

            
while sore>0:
    func2()
    sore-=1
   
easygui.msgbox('游戏结束!')

wyz601 发表于 2020-4-13 22:31:13

admintest166 发表于 2020-4-13 22:22
你这个问题很简单secret = random.randint(1, 10)中是一个int类型的 而你传给他的guess是一个字符串 st ...

谢谢,改为 func1(guess)就好了,忘了改变量
页: [1]
查看完整版本: 怎样解决这个错误