Python 2.7 为什么while循环不起作用?

Python 2.7 为什么while循环不起作用?,python-2.7,while-loop,Python 2.7,While Loop,我正在创建一个包含用户等级(a-F)、健康(0-100)和经济产出(0-100)的程序。我需要一个while循环,当用户输入一个错误的值时,例如一个健康值。为什么循环不断重复?我该如何在成绩上做到这一点 name = raw_input(' Enter your name: ') grade = raw_input(' Enter your grade: ') string_two = raw_input(' Enter your economic out put: ') while st

我正在创建一个包含用户等级(a-F)、健康(0-100)和经济产出(0-100)的程序。我需要一个while循环,当用户输入一个错误的值时,例如一个健康值。为什么循环不断重复?我该如何在成绩上做到这一点

name = raw_input(' Enter your name: ')

grade = raw_input(' Enter your grade: ')


string_two = raw_input(' Enter your economic out put: ')
while string_two not in range (0,100):
    print 'please enter a value between 0 and 100.'
    string_two = raw_input(' Enter your economic out put: ')

string_one = raw_input(' Enter your health: ')
while string_one not in range (0,100):
    print 'please enter a value between 0 and 100.'
    string_one = raw_input(' Enter your health: ')


health == int(string_one)
economic_output == int(string_two)

if economic_output > 85:
    print name + ' you are exceptional! Welcome aboard!'
elif grade == 'A':
     print name + ' you are exceptional! Welcome aboard!'
elif economic_output > 60:
    if health > 60:
        if grade == 'B' or 'C':
           print 'Congatulations ' + name +'!' + ' Come aboard!'
else:
    print 'Sorry, ' + name + ' but you dont meet the criteria and cammot be permitted to board. '

while
循环不断重复,因为
raw\u input
始终返回字符串对象,而不管您是只键入数字还是键入其他内容<代码>字符串\u 2不在范围(0100)始终为真,因为字符串不能在任何整数范围内

所以你能做什么。如果我是你,我会定义一个函数,要求用户在
while
循环中输入一些内容,直到他的输入满足某种模式(就像你在代码中尝试做的那样),并在必要时将其转换为特定类型。有一个非常有用的工具来检查字符串是否与某种模式匹配,它称为正则表达式(RE)

将一段代码多次用于函数是一种很好的做法。因此,我在这里定义了一个名为
inputAndValidate
的函数,其中包含两个必需的参数-一个提示,用于向用户显示,让用户了解您希望从他那里得到什么,以及检查其输入的模式。第三个参数是可选的——它是一个应用于用户输入的函数。默认情况下,它是
str
,一个将某物转换为字符串的函数。用户输入已经是一个字符串,因此此函数将不执行任何操作,但是如果需要int,我们将传递
int
作为第三个参数,并从
inputValidate
函数中获取int,依此类推

re.match(pattern,string)
如果字符串与模式匹配,则返回匹配对象,如果不匹配,则返回None。有关更多信息,请参见官方网站

函数的使用

name = inputAndValidate('Enter your name: ', r'.+') 
# This pattern means any string except empty one. Thus we ensure user will not enter an empty name.

grade = inputAndValidate('Enter your grade (A-F): ', r'[A-F]')
# This pattern means a string containing only one symbol in the A-F range (in the ASCII table).

economic_output = inputAndValidate('Enter your economic output (0-100): ', r'\d{1,3}', int)
# This pattern means a string containing from one to three digits. `int` passed to the third parameter means the result will be an int.

health = inputAndValidate('Enter your health (0-100): ', r'\d{1,3}', int)
# Same as the previous one.

re
模块文档还包含有关正则表达式语言的信息,它可以帮助您理解我使用的模式。

您是否也将函数复制到代码中?我指的是我在上面写的一段代码,它修复了while循环,但不是elif语句停止工作,它应该只接受B或C,但现在接受任何东西。我该怎么解决呢?看来情况不是你想要的。它是
(等级=='B')或('C')
(操作员优先级)<代码>'C'始终转换为true(除了空字符串外,隐式转换为bool时所有字符串都为true),因此整个条件永久为true<代码>成绩在['B','C']条件下应该有效。不打印祝贺,但也不打印其他位?啊,是的。您的
else
仅适用于最外层的
if
elif
s。它不适用于像检查成绩那样的内部考试。要使
其他
正常工作,您应将所有这三个条件与
放在一起,例如
elif economic_output>60和health>60,并在['B','C']中评级:
。如果看起来太长,可以使用反斜杠符号进行换行。或者,您可以创建一个函数来检查所有内容,并在
elif
name = inputAndValidate('Enter your name: ', r'.+') 
# This pattern means any string except empty one. Thus we ensure user will not enter an empty name.

grade = inputAndValidate('Enter your grade (A-F): ', r'[A-F]')
# This pattern means a string containing only one symbol in the A-F range (in the ASCII table).

economic_output = inputAndValidate('Enter your economic output (0-100): ', r'\d{1,3}', int)
# This pattern means a string containing from one to three digits. `int` passed to the third parameter means the result will be an int.

health = inputAndValidate('Enter your health (0-100): ', r'\d{1,3}', int)
# Same as the previous one.