首页 > 解决方案 > 为什么我不能显示这个 if 语句?

问题描述

所以我需要确保我输入的信息是否小于、大于或等于。不确定变量是否混乱。这是代码:

#the main function
def main():
    print   #prints a blank line
    age = getAge ()
    weight = getWeight()
    birthMonth = getMonth()
    print
    correctAnswers(age, weight, birthMonth)

#this function will input the age
def getAge():
    age = input('Enter your guess for age: ')
    return age

#thisfunction will input the weight
def getWeight():
    weight = input('Enter your guess for weight: ')
    return weight

#thisfunction will input the age
def getMonth():
    birthMonth = raw_input('Enter your guess for birth month: ')
    return birthMonth

#this function will determine if the values entered are correct
def correctAnswers(age, weight, birthMonth):
    if age <= 25:
        print 'Congratulations, the age is 25 or less.'

    if weight >= 128:
        print 'Congatulations, the weight is 128 or more.'

    if birthMonth == 'April':
        print 'Congratulations, the birth month is April.'

#calls main
main()

标签: pythonif-statement

解决方案


input()函数返回一个字符串。在执行您尝试执行的整数比较之前,您必须转换字符串。

例如:

def getAge():
    age = input('Enter your guess for age: ')
    return int(age)

推荐阅读