【问题标题】:python 3 getting error type error '<' not supported between instances of 'int and 'str'python 3在'int和'str'的实例之间不支持错误类型错误'<'
【发布时间】:2018-08-26 16:18:58
【问题描述】:

定义挑战(): Name = input('您好,请输入您的姓名?:') 而真: choice = input('Hi '+Name+' 有 30 个挑战需要复核 请在 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 之间输入您的选择:') 如果选择 == '1': user_age = int(input('你多大了?:')) print ('你是',user_age,'岁')

    elif choice == '2':
        user_num1 = int(input('Hi '+Name+' please enter your first number: '))
        user_num2 = int(input('Please enter your second number: '))
        total = user_num1+user_num2
        average = total/2
        print (average)

    elif choice == '3':
        width = int(input('Please enter your width: '))
        height = int(input('Please enter your height: '))
        area = width*height
        print ('the area of your rectangle is',area,'cm')

    elif choice == '4':
        user_num3= int(input(+Name+' please enter a number: '))
        user_num4= int(input('Now enter a second number: '))
        div= user_num3/user_num4
        print(div)

    elif choice == '5':
        Name = input('Hello Please enter your name?: ')
        user_fav_sub = input('What is your favourite subject?: ')
        print ('OMG '+user_fav_sub+' is my favourite aswell')

    elif choice == '6':
        Name = input ('Hello what is your name?: ')
        if Name =='Zak':
            print ('You\'re cool')
        else:
            print ('Nice to meet you')

    elif choice == '7':
        user_tv = input ('Hi '+Name+' how long do you spend watching TV?: ')
        if user_tv <= '2':
            print ('That shouldn\'t rit your brain too much')
        elif user_tv<='4':
            print ('Aren\'t you getting square eyes')
        else:
            print ('Fresh air beats channel flicking')

    elif choice == '8':
        user_mark = int(input('Hi '+Name+' how many marks did you get on your test?: '))
这就是问题所在
        if user_mark < '35':
           print ('You got a grade D')
        elif user_mark>='35':
            print ('You got a grade C')
        elif user_mark>='60':
            print ('You got a grade B')
        else:
            print('You got an A')




    else:
        print('Sorry incorect input please try again')

【问题讨论】:

  • user_mark 它是字符串,检查相等性,首先转换这些数字。
  • 我们无法将整数与字符串进行比较 - user_mark &lt; '35'
  • 请使用编辑器格式化您的问题,全部作为代码。由于缩进在 Python 中至关重要,因此请确保您的问题准确地显示了您运行的代码。

标签: python python-3.x typeerror


【解决方案1】:

您正在将字符串与整数进行比较。 user_mark 是一个 int,你可以将它与一个字符串进行比较。

从 if else 块中删除引号。

    if user_mark < 35:
       print ('You got a grade D')
    elif user_mark>=35:
        print ('You got a grade C')
    elif user_mark>=60:
        print ('You got a grade B')
    else:
        print('You got an A')

【讨论】:

  • ('100'&lt;'35') is True,所以第一种方法行不通。
  • 如果你直接比较字符串,你正在做类似于C的strcmp()
【解决方案2】:

你不检查用户的输入...

下面的代码应该适合你:

def isInt(value):
  try:
    int(value)
    return True
  except ValueError:
    print('Sorry, must enter a number...')
    return False

def intInRange(value,num1, num2):
    if isInt(value):
        value = int(value)
        if value in range(num1, num2):
            return True
        print('Enter a number between:',num1,'to',num2)
    return False


def challenges():
    Name = input('Hello Please enter your name:     ')
    print('Hi ' + Name + ', there are 8 challenges to review')
    choice = 'Go'
    while not choice == 'Exit':
        options = '\n*** Options ***' \
               '\n  1. How old are you?' \
               '\n  2. Average' \
               '\n  3. Rectangle area' \
               '\n  4. Divide' \
               '\n  5. Favorite' \
               '\n  6. What is your name?' \
               '\n  7. How much time do you spend on T.V?' \
               '\n  8. Grade check' \
               '\n  To stop, enter Exit'

        print(options)

        choice = input('\nPlease enter your choice between 1,2,3,4,5,6,7,8: ')
        while not intInRange(choice, 1, 15):
            print(options)
            choice = input('\nPlease enter your choice between 1,2,3,4,5,6,7,8: ')

        choice = int(choice)
        if choice == 1:
            user_age = input('How old are you?: ')
            while not isInt(user_age):
                user_age = input('How old are you?: ')
            print ('You are',user_age,'years old')

        elif choice == 2:
            user_num1 = input('Hi ' + Name + ' please enter your first number: ')
            while not isInt(user_num1):
                user_num1 = input('Hi ' + Name + ' please enter your first number: ')
            user_num2 = int(input('Please enter your second number: '))
            while not isInt(user_num2):
                user_num2 = int(input('Please enter your second number: '))

            total = int(user_num1) + int(user_num2)
            average = total / 2
            print(average)

        elif choice == 3:
            width = input('Please enter your width: ')
            while not isInt(width):
                width = input('Please enter your width: ')

            height = input('Please enter your height: ')
            while not isInt(height):
                height = input('Please enter your height: ')
            area = int(width) * int(height)
            print('The area of your rectangle is', area, 'cm')

        elif choice == 4:
            user_num3 = input(Name + ' please enter a number: ')
            while not isInt(user_num3):
                user_num3 = input(Name + ' please enter a number: ')

            user_num4 = input('Now enter a second number: ')
            while not isInt(user_num4):
                user_num4 = input('Now enter a second number: ')
            div = int(user_num3) / int(user_num4)
            print('The first number divided by the second number =',div)

        elif choice == 5:
            Name = input('Hello Please enter your name?: ')
            user_fav_sub = input('What is your favourite subject?: ')
            print('OMG ' + user_fav_sub + ' is my favourite as well')

        elif choice == 6:
            Name = input('Hello what is your name?: ')
            if Name == 'Zax':
                print('You\'re cool')
            else:
                print('Nice to meet you')

        elif choice == 7:
            user_tv = input('Hi ' + Name + ' how long do you spend watching TV?: ')
            while not isInt(user_tv):
                user_tv = input('Hi ' + Name + ' how long do you spend watching TV?: ')
            user_tv = int(user_tv)
            if user_tv <= 2:
                print('That shouldn\'t rit your brain too much')
            elif user_tv <= 4:
                print('Aren\'t you getting square eyes')
            else:
                print('Fresh air beats channel flicking')

        elif choice == 8:
            user_mark = input('Hi ' + Name + ' how many marks did you get on your test?: ')
            while not intInRange(user_mark,0,101):
                user_mark = input('Hi ' + Name + ' how many marks did you get on your test?: ')

            user_mark = int(user_mark)
            if user_mark < 35:
                print('You got a grade D')
            elif user_mark in range(35,60):
                print('You got a grade C')
            elif user_mark in range(60,80):
                print('You got a grade B')
            else:
                print('You got an A')

        else:
            print('Sorry incorrect input please try again')

challenges()

【讨论】:

    【解决方案3】:
    def challenges():
        Name = input('Hello Please enter your name?: ')
        while True:
            choice = input('Hi '+Name+' there are 30 challenges to reveiw please enter your choice between 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15: ')
            if choice == '1':
                user_age = int(input('How old are you?: '))
                print ('You are',user_age,'years old')
    
        elif choice == '2':
            user_num1 = int(input('Hi '+Name+' please enter your first number: '))
            user_num2 = int(input('Please enter your second number: '))
            total = user_num1+user_num2
            average = total/2
            print (average)
    
        elif choice == '3':
            width = int(input('Please enter your width: '))
            height = int(input('Please enter your height: '))
            area = width*height
            print ('the area of your rectangle is',area,'cm')
    
        elif choice == '4':
            user_num3= int(input(+Name+' please enter a number: '))
            user_num4= int(input('Now enter a second number: '))
            div= user_num3/user_num4
            print(div)
    
        elif choice == '5':
            Name = input('Hello Please enter your name?: ')
            user_fav_sub = input('What is your favourite subject?: ')
            print ('OMG '+user_fav_sub+' is my favourite aswell')
    
        elif choice == '6':
            Name = input ('Hello what is your name?: ')
            if Name =='Zak':
                print ('You\'re cool')
            else:
                print ('Nice to meet you')
    
        elif choice == '7':
            user_tv = input ('Hi '+Name+' how long do you spend watching TV?: ')
            if user_tv <= '2':
                print ('That shouldn\'t rit your brain too much')
            elif user_tv<='4':
                print ('Aren\'t you getting square eyes')
            else:
                print ('Fresh air beats channel flicking')
    
        elif choice == '8':
            user_mark = input('Hi '+Name+' how many marks did you get on your test?: ')
            if user_mark < '35':
               print ('You got a grade D')
            elif user_mark>='35':
                print ('You got a grade C')
            elif user_mark>='60':
                print ('You got a grade B')
            else:
                print('You got an A')
    
    
    
    
        else:
            print('Sorry incorect input please try again')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 2019-05-09
      • 2019-10-24
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      相关资源
      最近更新 更多