Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python初学者-TypeError:'<';在';str';和';int';_Python_Python 3.x - Fatal编程技术网

Python初学者-TypeError:'<';在';str';和';int';

Python初学者-TypeError:'<';在';str';和';int';,python,python-3.x,Python,Python 3.x,非常感谢你阅读我的第一篇文章 开始学习Python 3.6.1-刚开始时就被卡住了-以下代码有什么问题: print('Hi there! What is your name?') myName = input() print("Hello " + myName + ' its good to met you. My name is Kendo.') print('how old are you?') myAge = input() if myAge < 15: prin

非常感谢你阅读我的第一篇文章

开始学习Python 3.6.1-刚开始时就被卡住了-以下代码有什么问题:

print('Hi there! What is your name?')
myName = input()
print("Hello "   +  myName +  ' its good to met you. My name is Kendo.')

print('how old are you?')
myAge = input()
if myAge < 15:
    print('go to bed, kiddo')
elif myAge > 95:
    print('Sup, grandma')
elif myAge > 1000:
    print('Lol, stop kidding me')
print('你好!你叫什么名字?')
myName=input()
打印(“你好”+我的名字+“很高兴认识你。我的名字是剑道。”)
打印('你多大了?')
myAge=input()
如果我的年龄<15岁:
打印(‘去睡觉,孩子’)
elif myAge>95:
打印(“支持,奶奶”)
elif myAge>1000:
打印(‘哈哈,别跟我开玩笑了’)

您的输入是一个字符串,您需要将其转换为int以使用比较运算符

而不是:

print('how old are you?')
myAge = input()
试试这个:

myAge = int(input('How old are you?')

问题是您需要一个整数,但input()返回一个字符串。 您可以使用如下方式将输入转换为int:

对于Python 3.x

myAge  = int(input("Enter a number: "))
myAge = input("Enter a number: ")
>>>Enter a number: 5 + 17

myAge, type(myAge)
(22, <type 'int'>)
对于Python 2.x:

myAge  = int(input("Enter a number: "))
myAge = input("Enter a number: ")
>>>Enter a number: 5 + 17

myAge, type(myAge)
(22, <type 'int'>)
myAge=input(“输入一个数字:”)
>>>输入一个数字:5+17
myAge,类型(myAge)
(22, )

输入返回python 3中的字符串对象。您正在尝试查看字符串是小于还是大于整数。这是行不通的

来自Python3.6文档 输入([提示]) 如果存在prompt参数,则将其写入标准输出,而不带尾随换行符。然后,函数从输入中读取一行,将其转换为字符串(去掉尾随的换行符),并返回该字符串

尝试:


但是请注意,当您在
myAge=input()
中输入任何非数字字符时,这将引发另一个错误。因为您不能强制将非数字字符转换为整数。

请看一看,它涵盖了Python 2和3。@Marko Petkovic我给出了一个涵盖Python 2.x和3.x的小回答