python 3 getting error type error '<' not supported between instances of 'int and 'str'
def挑战():name=input('您好,请输入您的姓名?:'如果为真:choice=input('hi'+name+'有30个挑战需要查看。请输入您在1、2、3、4、5、6、7、8、9、10、11、12、13、14、15'之间的选择:')如果choice='1':user_age=int(input('您多大了?:")print('您是',用户年龄,'岁')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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?: ')) |
这就是问题所在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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') |
号
您正在将字符串与整数进行比较。用户标记是一个int,您可以将它与字符串进行比较。
从if else块中删除引号。
1 2 3 4 5 6 7 8 | 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') |
您不检查用户的输入…
下面的代码适用于您:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 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 = ' *** Options ***' \ ' 1. How old are you?' \ ' 2. Average' \ ' 3. Rectangle area' \ ' 4. Divide' \ ' 5. Favorite' \ ' 6. What is your name?' \ ' 7. How much time do you spend on T.V?' \ ' 8. Grade check' \ ' To stop, enter Exit' print(options) choice = input(' Please enter your choice between 1,2,3,4,5,6,7,8: ') while not intInRange(choice, 1, 15): print(options) choice = input(' Please 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() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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') |
号