python 想让它更短,而“while”现在不起作用

e5nqia27  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(153)

我有这样的代码:

num1, check = None, None
proceed_answer = input('Great, we have one more game, wanna play? (y/n): ')

if proceed_answer == 'y':
    while True:
        if num1 is None or not check_valid_number(num1):
            try:
                num1 = float(input('Enter one valid number: '))
            except ValueError:
                print('It must be a number!')
                continue
            if not check_valid_number(num1):
                print('It must be greater than 0!')
                continue
        if check is None or not check_valid_number(check):
            try:
                check = float(input('Enter one more valid number: '))
            except ValueError:
                print('It must be a number!')
                continue
            if not check_valid_number(check):
                print('It must be greater than 0!')
                continue
        if even_division(num1, check):
            print(f'{check} divides evenly into {num1}')
        else:
            print(f'{check} does not divide evenly into {num1}')
        break
elif proceed_answer == 'n':
    print('Okay, bye')

但我想让它更复杂,因为我认为它有点混乱,我想出了这个,它似乎不会在VS代码中出现任何错误:

num1, check = None, None
proceed_answer = input('Great, we have one more game, wanna play? (y/n): ')

if proceed_answer.lower() == 'y':
    while num1 is None or not check_valid_number(num1):
        try:
            num1 = float(input('Enter one valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(num1):
            print('It must be greater than 0!')
            continue
        try:
            check = float(input('Enter one more valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(check):
            print('It must be greater than 0!')
            continue
        if even_division(num1, check):
            print(f'{check} divides evenly into {num1}')
        else:
            print(f'{check} does not divide evenly into {num1}')
else:
    print('Okay, bye')

它运行得很好,直到第二次尝试,如果我输入一个小于0的数字或一个字母,它会显示相应的消息,但程序停止。我添加了continue,所以我不明白为什么它会停止。第一个代码工作正常,第二个带来了我所描述的错误。
功能:

def check_valid_number(data):
    return 0 <= data

def even_division(inp1, inp2):
    return inp1 % inp2 == 0

我知道check_valid_number函数是不必要的,但我想尽可能多地练习函数。
谢谢大家。
我希望这两个“版本”的工作方式是一样的。我试着问chat gpt,但他开始编写更困难的代码。

qybjjes1

qybjjes11#

您缺少check变量的while语句。如果check的值无效或为None,continue将指向返回到此while语句。

num1, check = None, None
proceed_answer = input('Great, we have one more game, wanna play? (y/n): ')
if proceed_answer.lower() == 'y':
    while num1 is None or not check_valid_number(num1):
        try:
            num1 = float(input('Enter one valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(num1):
            print('It must be greater than 0!')
            continue
    while check is None or not check_valid_number(check):   
        try:
            check = float(input('Enter one more valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(check):
            print('It must be greater than 0!')
            continue
    if even_division(num1, check):
        print(f'{check} divides evenly into {num1}')
    else:
        print(f'{check} does not divide evenly into {num1}')
else:
    print('Okay, bye')

相关问题