Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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“while not”脚本_Python_While Loop - Fatal编程技术网

这个代码有什么问题?Python“while not”脚本

这个代码有什么问题?Python“while not”脚本,python,while-loop,Python,While Loop,我想让程序检查代码是否为“w”、“w”、“d”或“d”,然后从那里指导用户,但实际上,即使输入为“w”、“w”、“d”或“d”,它也总是无效。我不想使用'while code等于'w','w','d'或'd'continue on',因为即使这在程序运行时起作用,它也会导致程序在完成后从头开始 另外,我不允许使用if语句进行简单的操作 if code == 'W' or code == 'w' or code == 'D' or code == 'd': prevbalance = fl

我想让程序检查代码是否为“w”、“w”、“d”或“d”,然后从那里指导用户,但实际上,即使输入为“w”、“w”、“d”或“d”,它也总是无效。我不想使用'while code等于'w','w','d'或'd'continue on',因为即使这在程序运行时起作用,它也会导致程序在完成后从头开始

另外,我不允许使用if语句进行简单的操作

if code == 'W' or code == 'w' or code == 'D' or code == 'd':
    prevbalance = float(input('Enter your previous balance: $'))
else:
    invalid()
以下是问题所在:

code = input('Enter your transaction code: ')
while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
    invalid()
prevbalance = float(input('Enter your previous balance: $'))
以下是整个程序(如果有帮助):

def invalid():
    input('Invalid transaction code. Please select another code.')
    main()

def deposit(prevbalance, amount, code):
    Wbalance = ''
    Dbalance = prevbalance + amount
    balance(Wbalance, Dbalance, code)

def withdrawal(prevbalance, amount, code):
    Dbalance = ''
    Wbalance = prevbalance - amount
    if amount > prevbalance:
        print('~~ERROR~~  You cannot withdrawal more than you have')
        input('           Please try a lower amount back at the main menu')
        main()
    else:
        balance(Wbalance, Dbalance, code)

def balance(Wbalance, Dbalance, code):
    if code == "W" or code == "w":
        print('Your new balance is: $',format(Wbalance, ',.2f'),)
    else:
        print('Your new balance is: $',format(Dbalance, ',.2f'),)

def main():
    name = input('Enter your name: ')
    ID = input('Enter your account ID: ')
    code = input('Enter your transaction code: ')
    while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
        invalid()
    prevbalance = float(input('Enter your previous balance: $'))
    amount = float(input('Enter your transaction amount: $'))
    if code == "W" or code == "w":
        withdrawal(prevbalance, amount, code)
    elif code == "D" or code == "d":
            deposit(prevbalance, amount, code)

main()
input('Press ENTER to continue...')
在while条件中,not将仅否定code=='W'而不是其他条件

因此,您可能需要执行以下操作:

while code not in ('W', 'w', 'D', 'd'):
不是在你所有的情况下都有。您需要添加括号:

while not (code == 'W' or code == 'w' or code == 'D' or code == 'd'):
while not (code == 'W' or code == 'w' or code == 'D' or code == 'd'):
或者更好地使用in运算符:

while code not in ('W', 'w', 'D', 'd'):

这是一个运算符优先级问题。在以下方面:

while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
while code not in ('W', 'w', 'D', 'd'):
not运算符比or绑定得更紧密,因此它相当于:

while (not code == 'W') or code == 'w' or code == 'D' or code == 'd':
这不是我们的初衷。您可以使用括号修复它:

while not (code == 'W' or code == 'w' or code == 'D' or code == 'd'):
while not (code == 'W' or code == 'w' or code == 'D' or code == 'd'):
或者使用德摩根定律:

while code != 'W' and code != 'w' and code != 'D' and code != 'd':
但我建议如下:

while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
while code not in ('W', 'w', 'D', 'd'):

希望有帮助。

无效函数应将控制返回调用点,而不调用main.while code.lower.strip不在“wd”中?请在此线程中查看解决方案。输入关键字可能不符合您的想法,而代码不在“WwDd”中:也将接受wD?非常感谢!!我很困惑为什么它不起作用。我试过“!=”,然而,它也不起作用,尽管我可能把它放错了。不管怎样,再次感谢您的帮助!