输入表单验证

时间:2015-11-22 09:33:53

标签: python validation input

我必须做一个程序,要求输入,直到用户输入正确的输入。输入必须采用[text] [number]的形式。

x = ''
while hereshouldbevalidationconditions:
    x = input("Enter your data in the form of [text] [number]: ")

3 个答案:

答案 0 :(得分:1)

如:Emulate a do-while loop in Python?

中所述

使用此,

while True:
  x = input("Enter your data in the form of [text] [number]: ")
  if some_is_valid_condition:
    break

答案 1 :(得分:0)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SpeechRecognizer</groupId>
  <artifactId>SpeechRecognizer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.3</source>
          <target>1.2</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-data</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-samples</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

此处代码检查是否有超过2个参数解析为x(由空格分隔)。第一个也应该是一个数字。

我希望它有所帮助,祝你好运。

答案 2 :(得分:0)

谢谢,分裂是个好主意。但问题是,当用户输入[text] [number]且[和]之间没有空格时,它还必须要求输入新的输入。 split(),这里给出错误......

#0  0x0000000000000000 in ?? ()
#1  0x00007ffff33fb6fd in init () at dlerror.c:177
#2  _dlerror_run (operate=operate@entry=0x7ffff33fb0e0 <dlsym_doit>,args=args@entry=0x7fffffffde00) at dlerror.c:129
#3  0x00007ffff33fb148 in __dlsym (handle=<optimized out>, name=optimized out>) at dlsym.c:70
#4  0x00007ffff6fa2e1e in ?? () from /usr/lib/nvidia-352/libGL.so.1
#5  0x00007ffff6f4db47 in ?? () from /usr/lib/nvidia-352/libGL.so.1
#6  0x00007ffff7de957d in call_init (l=0x7ffff7fc59c8,argc=argc@entry=1,         argv=argv@entry=0x7fffffffdf58, env=env@entry=0x7fffffffdf68)at dl-init.c:58
#7  0x00007ffff7de96cb in call_init (env=<optimized out>, argv=<optimized out>, argc=<optimized out>, l=<optimized out>) at dl-init.c:30
#8  _dl_init (main_map=0x7ffff7ffe188, argc=1, argv=0x7fffffffdf58, env=0x7fffffffdf68) at dl-init.c:120
#9  0x00007ffff7dd9d0a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#10 0x0000000000000001 in ?? ()
#11 0x00007fffffffe2c8 in ?? ()
#12 0x0000000000000000 in ?? ()