简体   繁体   English

Python 多条件输入验证:语法正确?

[英]Python Input Validation With Multiple Conditions: Correct Syntax?

I've created a simple program where the user is prompted to guess a number between 1 and 20. Originally my program just prompted the user if their input was too high or low.我创建了一个简单的程序,提示用户猜测 1 到 20 之间的数字。最初,我的程序只是提示用户输入是太高还是太低。 Later on I added a function that prompted the user when their input is out of range (below 1 or above 20).后来我添加了一个 function,当用户的输入超出范围(低于 1 或高于 20)时会提示用户。 The program works fine, however I was wondering if my format and syntax are "correct" or "proper".该程序运行良好,但是我想知道我的格式和语法是“正确”还是“正确”。

magic_number = 12

# get input from user 
user_number = int(input('What is the magic number between 1 and 20 ? '))

# attempting to create an efficient while loop
while True:
    if user_number < 12 and user_number >= 1:
        # error message 
        print('Your number is too low. Please try again.')
        # ask for input again
        user_number = int(input('What is the magic number between 1 and 20 ? '))
    elif user_number > 12 and user_number <= 20:
        # error message
        print('Your number is too high. Please try again.')
        # ask for input again
        user_number = int(input('What is the magic number between 1 and 20? '))
    elif user_number < 1 or user_number > 20:
        # error message
        print('Your number is out of range. Please try again.')
        # ask for input again
        user_number = int(input('What is the magic number between 1 and 20? '))
    elif user_number == magic_number:
        print('Congratulations! You have guessed the magic number.')
        break

If you're interested in the official "Pythonic" way to format your code, that's what the PEP8 specification is all about: https://www.python.org/dev/peps/pep-0008/ .如果您对格式化代码的官方“Pythonic”方式感兴趣,这就是 PEP8 规范的全部内容: https://www.python.org/dev/peps/pep-0008/

Looking at your code, I have a couple of suggestions to possibly make things a little cleaner and easier to maintain:查看您的代码,我有一些建议可以使事情变得更清洁和更易于维护:

  1. Try to avoid hard-coding the same constant value (eg 1, 20, 12) in multiple places.尽量避免在多个位置硬编码相同的常量值(例如 1、20、12)。 That way if you want to change them later, you only have to change it in one place.这样,如果您想稍后更改它们,您只需在一个地方进行更改。
  2. Try to avoid repeat logic in multiple places if you can help it (or move it into a function) for the same reason as #1.如果您可以帮助它(或将其移动到函数中),请尽量避免在多个地方重复逻辑,原因与 #1 相同。 If you have a bug you only have to fix it in one spot.如果你有一个错误,你只需要在一个地方修复它。

There's a lot of things you could do, but here's an example of what it might look like just following those two comments:您可以做很多事情,但这里有一个示例,说明仅遵循这两条评论可能会是什么样子:

magic_number = 12

min_number = 1
max_number = 20

# attempting to create an efficient while loop
while True:
    # get input from user
    user_number = int(input('What is the magic number between {} and {} ? '.format(min_number, max_number)))

    if user_number < magic_number and user_number >= min_number:
        print('Your number is too low. Please try again.')
    elif user_number > magic_number and user_number <= max_number:
        print('Your number is too high. Please try again.')
    elif user_number < min_number or user_number > max_number:
        print('Your number is out of range. Please try again.')
    elif user_number == magic_number:
        print('Congratulations! You have guessed the magic number.')
        break

Maybe some future things to try out might be figuring out how to handle if the user doesn't enter a number or using the random module to generate a different magic number each time instead of hard-coding it to 12.也许将来要尝试的一些事情可能是弄清楚如果用户不输入数字时如何处理,或者每次使用random模块生成不同的幻数而不是将其硬编码为 12。

Good luck and hope that helps.祝你好运,希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM