Python验证用户输入的数据问题

2024-03-29 14:51:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,我在做家庭作业时遇到了麻烦。每次我运行代码并输入数据时,我总是在输入数字时陷入困境。它会显示我的“您输入的2”,这是项目年度增长金额的错误值

请以小数形式输入您的项目年增长额,如2代表2%,45代表45%:“

##4.  Ask the user to enter the project annual increase
ProjectAnnIncrease = input('Please enter the your project annual increase in decimal form such as 2 for 2% or 45 for 45%: \n')


##4a. Validate the user entered data that is a decimal and a positive decimal less than .50
while not ProjectAnnIncrease.isdigit() or float(ProjectAnnIncrease) > 0 or float(ProjectAnnIncrease) < 50:
    print('You entered', (ProjectAnnIncrease) , 'which is an incorrect value for the project annual increase amount.\n')
    (ProjectAnnIncrease) = input('Please enter your project annual increase amount in decimal form such as 2 for 2% or 45 for 45%: \n')```

1条回答
网友
1楼 · 发布于 2024-03-29 14:51:38

你需要改变你的对比符号相反,你想继续问你是低于0还是高于50

while not ProjectAnnIncrease.isdigit() or float(ProjectAnnIncrease) < 0 or float(ProjectAnnIncrease) > 50:

相关问题 更多 >