【问题标题】:Integer Validation Python整数验证 Python
【发布时间】:2016-08-04 05:00:41
【问题描述】:

我需要在 while 循环中添加验证。

但是,当我使用此验证时,它不起作用,而只会出现错误消息,当我希望它提供验证错误消息并让用户再试一次。

我不知道在 while 循环中使用它是否会使我使用的验证有所不同,是吗?

我还需要将此“def inputNumber(message):”更改为我的输入存储的内容吗?

这个“userInput = int(input(message))”我的输入存储为什么?

import time 
import random 
question = 0 
score = 0 
name = input("What is your full name?") 
print ("Hello " + name, "welcome to The Arithmetic Quiz. Use integers to enter the answer!") 
time.sleep(2) 
operands1 = list(range(2, 12)) 
operators = ["+","-","x"] 
operands2 = list(range(2, 12)) 

while question < 10:  
    operand1 = random.choice(operands1)
    operand2 = random.choice(operands2) 
    operator = random.choice(operators) 
    def inputNumber(message):
        while True:
            try:
               userInput = int(input(message))       
            except ValueError:
                print("Not an integer! Try again.")
                continue
            else:
                return userInput 
            break
    user_answer =int(input('{} {} {} = '.format(operand1, operator, operand2)))

【问题讨论】:

  • 在最后一行,我想你想做user_answer =int(inputNumber('{} {} {} = '.format(operand1, operator, operand2))),但是却做了user_answer =int(input('{} {} {} = '.format(operand1, operator, operand2)))
  • def inputNumber(message) 在循环的每次迭代中定义一个名为 inputNumber 的函数。你实际上并没有调用它。
  • @muhammadtahir 我现在已经改变了它,现在它可以工作了。谢谢!

标签: python validation while-loop integer


【解决方案1】:

我怀疑你是否想像你在这里做的那样在一个 while 循环中定义你的函数:

while question < 10:
    ... 
    def inputNumber(message): 
        ...

相反,您可以在循环之外定义函数,然后从其他地方的循环中调用它 x 次。例如

def inputNumber(message): 
    ...
    return userInput

while question < 10:
    # pick random numbers/operators
    ...
    # call inputNumber() with numbers/operators as message. Return user_answer
    user_answer = int(inputNumber('{} {} {} = '.format(operand1, operator, operand2)))
    # check if the answer is correct
    ...
    # increment question so it doesn't run infinitely
    question += 1

【讨论】:

    【解决方案2】:

    @user6104134 已经解决了这个问题;但是,我想为遇到类似问题的其他人提供答案。

    试试这个解决方案

    import random
    import time
    
    question = 0
    score = 0
    
    
    def inputnumber(prompt):
        while True:
            response = raw_input(prompt)
            try:
                if isinstance(response, int):
                    return int(response)
                else:
                    print "Not an integer! Try again."
            except ValueError:
                print "Not an integer! Try again."
    
    
    name = raw_input("What is your full name? ")
    print ("Hello " + name, "welcome to The Arithmetic Quiz. Use integers to enter the answer!")
    time.sleep(2)
    operands1 = list(range(2, 12))
    operators = ["+", "-", "x"]
    operands2 = list(range(2, 12))
    
    while question < 10:
        operand1 = random.choice(operands1)
        operand2 = random.choice(operands2)
        operator = random.choice(operators)
        user_answer = int(inputnumber('{} {} {} = '.format(operand1, operator, operand2)))
        question += 1
    

    问题

    首先,您应该在脚本之外声明函数定义并通过标识符“inputNumber()”调用函数

    还要注意 Try/Except 的细微变化,以及符合 PEP 8 Style Guide 的格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多