首页 > 解决方案 > python 3在'int和'str'的实例之间不支持错误类型错误'<'

问题描述

def challenge(): Name = input('Hello Please enter your name?:') while True: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('你多大了?: ')) 打印('你是',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')

标签: pythonpython-3.xtypeerror

解决方案


您正在将字符串与整数进行比较。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')

推荐阅读