Python3获取错误类型error'<';在';int和';str';
def挑战(): Name=input('您好,请输入您的姓名?:') 尽管如此: choice=输入('Hi'+名称+'需要查看的挑战有30个I请输入您在1,2,3,4,5,6,7,8,9,10,11,12,13,14,15之间的选择:')) 如果选项==“1”: user_age=int(输入('你几岁?:')) 打印('您是',用户年龄,'岁')Python3获取错误类型error'<';在';int和';str';,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,def挑战(): Name=input('您好,请输入您的姓名?:') 尽管如此: choice=输入('Hi'+名称+'需要查看的挑战有30个I请输入您在1,2,3,4,5,6,7,8,9,10,11,12,13,14,15之间的选择:')) 如果选项==“1”: user_age=int(输入('你几岁?:')) 打印('您是',用户年龄,'岁') elif choice==“2”: user_num1=int(输入('Hi'+Name+'请输入您的第一个数字:')) user_num2=i
elif choice==“2”:
user_num1=int(输入('Hi'+Name+'请输入您的第一个数字:'))
user_num2=int(输入('请输入第二个数字:'))
总计=用户数量m1+用户数量m2
平均数=总数/2
打印(平均)
elif选项==“3”:
宽度=int(输入('请输入宽度:'))
高度=int(输入('请输入您的高度:'))
面积=宽度*高度
打印('矩形的面积为',面积为'cm')
elif选项==“4”:
user_num3=int(输入(+Name+'请输入一个数字:'))
user_num4=int(输入('现在输入第二个数字:'))
div=user\u num3/user\u num4
印刷(部门)
elif选项==“5”:
Name=input('您好,请输入您的姓名?:')
user_fav_sub=input('你最喜欢的主题是什么?:')
打印('OMG'+用户\u fav\u sub+'也是我最喜欢的')
elif选项==“6”:
Name=input('你好,你叫什么名字?:')
如果名称=='Zak':
打印(“你很酷”)
其他:
打印(‘很高兴见到你’)
elif选项==“7”:
user_tv=input('Hi'+Name+'你看电视多长时间?:')
如果用户_tv='35':
打印('你得了C级')
elif用户标记>='60':
打印('你得了B级')
其他:
打印('你得了A')
其他:
打印('对不起,输入不正确,请重试')
您正在将字符串与整数进行比较。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')
如果用户标记<35:
打印('你得了D级')
elif用户标记>=35:
打印('你得了C级')
elif用户标记>=60:
打印('你得了B级')
其他:
打印('你得了A')
def challenges():
Name=input('您好,请输入您的姓名?:')
尽管如此:
choice=输入('Hi'+名称+'需要查看的挑战有30个I请输入您在1,2,3,4,5,6,7,8,9,10,11,12,13,14,15之间的选择:'))
如果选项==“1”:
user_age=int(输入('你几岁?:'))
打印('您是',用户年龄,'岁')
elif choice==“2”:
user_num1=int(输入('Hi'+Name+'请输入您的第一个数字:'))
user_num2=int(输入('请输入第二个数字:'))
总计=用户数量m1+用户数量m2
平均数=总数/2
打印(平均)
elif选项==“3”:
宽度=int(输入('请输入宽度:'))
高度=int(输入('请输入您的高度:'))
面积=宽度*高度
打印('矩形的面积为',面积为'cm')
elif选项==“4”:
user_num3=int(输入(+Name+'请输入一个数字:'))
user_num4=int(输入('现在输入第二个数字:'))
div=user\u num3/user\u num4
印刷(部门)
elif选项==“5”:
Name=input('您好,请输入您的姓名?:')
user_fav_sub=input('你最喜欢的主题是什么?:')
打印('OMG'+用户\u fav\u sub+'也是我最喜欢的')
elif选项==“6”:
Name=input('你好,你叫什么名字?:')
如果名称=='Zak':
打印(“你很酷”)
其他:
打印(‘很高兴见到你’)
elif选项==“7”:
user_tv=input('Hi'+Name+'你看电视多长时间?:')
如果用户_tv='35':
打印('你得了C级')
elif用户标记>='60':
打印('你得了B级')
其他:
打印('你得了A')
其他:
打印('对不起,输入不正确,请重试')
您不检查用户的输入
下面的代码应该适用于您:
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()
def isInt(值):
尝试:
int(值)
返回真值
除值错误外:
打印('对不起,必须输入一个数字…')
返回错误
def初始范围(值,num1,num2):
如果isInt(值):
value=int(值)
如果值在范围内(num1,num2):
返回真值
打印('输入介于:',num1,'to',num2之间的数字)
返回错误
def挑战():
Name=input('您好,请输入您的姓名:')
打印('Hi'+名称+',有8项挑战需要审查')
选择='Go'
而不是选择==“退出”:
选项='\n***选项***'\
“\n 1。你多大了?”\
“\n 2。平均值\
“\n 3。矩形区域'\
“\n 4。分开\
“\n 5。最爱的\
“\n 6。你叫什么名字?”\
“\n 7。你在电视上花了多少时间?”\
“\n 8。成绩检查'\
“\n要停止,请输入Exit”
打印(选项)
choice=输入('\n请在1,2,3,4,5,6,7,8之间输入您的选择:')
不在范围内时(选项1、15):
打印(选项)
choice=输入('\n请在1,2,3,4,5,6,7,8之间输入您的选择:')
choice=int(choice)
如果选项==1:
user_age=input('你几岁?:')
不存在时(用户年龄):
user_age=input('你几岁?:')
打印('您是',用户年龄,'岁')
elif选项==2:
user_num1=input('Hi'+Name+'请输入您的第一个号码:'))
不存在时(用户\u num1):
user_num1=input('Hi'+Name+'请输入您的第一个号码:'))
user_num2=int(输入('请输入第二个数字:'))
当不存在时(用户_
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')
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')
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()