Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.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 2.7 python根据用户输入打印输出_Python 2.7 - Fatal编程技术网

Python 2.7 python根据用户输入打印输出

Python 2.7 python根据用户输入打印输出,python-2.7,Python 2.7,这是一个简单的代码,脚本将询问用户的姓名、年龄、性别和身高,并基于此给出输出。我目前的代码如下: print "What is your name?" name = raw_input() print "How old are you?" age = raw_input() print "Are you male? Please answer Y or N" sex = raw_input() if sex == "Y" or sex == "y": sex = "Male" else:

这是一个简单的代码,脚本将询问用户的姓名、年龄、性别和身高,并基于此给出输出。我目前的代码如下:

print "What is your name?"
name = raw_input()
print "How old are you?"
age = raw_input()
print "Are you male? Please answer Y or N"
sex = raw_input()
if sex == "Y" or sex == "y":
    sex = "Male"
else:
    sex = "Female"
print "How tall are you? Please enter in feet such as 5.5"
height = raw_input()

if sex == "Male" and height >= 6.0:
    t = "You are tall for a male"
elif sex == "Male" and height < 6.0:
    t = "You are below average for a male"

elif sex == "Female" and height >= 5.5:
    t = "You are tall for a female"
else:
    t = "You are below average for a female"


print "Hello %s. You are %s years old and %s feet tall. %s." % (name, age, height, t)
我挂断了if,elif,else的声明:

if sex == "Male" and height >= 6.0:
    t = "You are tall for a male"
elif sex == "Male" and height < 6.0:
    t = "You are below average for a male"

elif sex == "Female" and height >= 5.5:
    t = "You are tall for a female"
else:
    t = "You are below average for a female"
代码将区分性别是男性还是女性,但始终会返回xxx的身高。我不知道如何获得You is below average返回值。

这是因为原始输入返回的是字符串,而不是浮点,在Python 2中,字符串和浮点之间的比较方式总是相同的

>>> "1.0" > 6.0
True
这样做:

height = float(raw_input())
高度=输入也会起作用,但由于评估的原因,不鼓励使用安全问题

注意:这在Python3中得到了修复,可能是因为它不是很有用,而且容易出错:尝试这样做会导致

TypeError: unorderable types: str() > float()
这是明确的,会让你意识到你的错误

注意:如果您试图比较年龄=原始输入,则同样的问题应该是年龄=内部输入