python - 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')
解决方案
您正在将字符串与整数进行比较。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')
推荐阅读
- sql - 如何过滤 Query Sql 使其在同一张表的多个行具有两个相同值时仅显示一个值?
- python-3.x - Unable to get the second line name as single word in pytesseract
- r - 使用 R 来获得上一个和明年没有几天的优雅方式?
- swift - Swift:NSCollectionViewItem 属性不变
- php - PHP-如何做 str_shuffle 产生不同的价值?
- python - 将所有数字分解为给定数字的快速算法
- python-3.x - 如何以经济实惠的方式为我的工作公司发布 Web 应用程序
- vue.js - 验证动态路由时使用“$router.push”
- c++ - 支持 CLion 中包含的目录
- go - 从通用且以某种方式动态的 go 地图获取内容的最佳方式是什么?