python - 为什么我不能显示这个 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()
解决方案
input()
函数返回一个字符串。在执行您尝试执行的整数比较之前,您必须转换字符串。
例如:
def getAge():
age = input('Enter your guess for age: ')
return int(age)
推荐阅读
- azure-active-directory - Azure App Svc 中 Blazor 应用的 Azure AD 应用服务授权
- azure-log-analytics - 无法使用 Azure Monitor 日志运行查询“AzureActivity”
- r - R,使用for循环,如何根据不同的目标数据框生成具有不同结果的新变量
- angular - Firebase 不返回对象
- python - 无法在 Spyder IDE 的变量资源管理器中打开数据框
- vue.js - 在 Vue3 组合 API 中让观察者立即工作
- c# - 如何使用 Dapper 检查空值
- reactjs - 从外部链接到 React PWA 的深度链接
- python - 突出显示python pandas to_latex中每一行的最佳值
- javascript - Highchart 地图/Highmaps 在点击国家时更新 vue 数据属性