在我的猜测游戏中验证用户输入为整数

时间:2019-04-05 23:25:57

标签: python

我有一个猜谜游戏,我想验证用户输入是否为数字,并且该数字必须在我的范围内(如果在此处甚至是正确的术语),如果不是,则返回一条错误消息,允许用户执行以下操作:再试一次。

我已经设置了猜谜游戏,但是现在我正试图对其设置限制。 我尝试使用isdigit(str),但是我不确定我是否完全理解它的工作原理,所以现在有点想让它与TRY和Except一起工作: 我还认为我弄乱了之前可以正常工作的代码,因为例如现在输入5时,它表示数字不在1到10之间,所以else语句是错误的。

import random

`enter code here`#Welcome Message
print("Welcome to my Guess the number program!")
print()

guess = ()
number = random.randint(1, 10)
count = 0

#User input/game rules
while True:
    guess = int(input("Guess a number between 1 and 10: "))
    try:
        guess = int(guess)
        if guess<1 or guess>10:
            count += 1
            guess = int(guess)
            print()
            if guess < number:
                print("Too low.")
                print()
            elif guess > number:
                print("Too high.")
                print()
            else:
                print("You guessed it!")
                print("You guessed it in" ,count, "attempts.")
        else:
            print("number not between 1 and 10")
    except:
        print("Invalid input")[enter image description here][1]

这是我开始弄混之前的代码。

import random

#Welcome Message
print("Welcome to my Guess the number program!")
print()

guess = ()
number = random.randint(1, 10)
count = 0

#User input/game rules
while guess != number:
    guess = int(input("Guess a number between 1 and 10: "))
    count += 1
    print()
    if guess < number:
        print("Too low.")
        print()
    elif guess > number:
        print("Too high.")
        print()
    elif guess == number:
        print("You guessed it!")
        print("You guessed it in" ,count, "attempts.")
    else:
        break

2 个答案:

答案 0 :(得分:0)

这是错误的

// Do not forget the Rule import at the top of your controller
use Illuminate\Validation\Rule;

// Then in your method
$this->validate(request(), [
    'name' => 'required',
    'username' => [
        'required',
        Rule::unique('users', 'username')->ignore($user)
    ]
]);

当猜测为if guess<1 or guess>10: 时,则为5,然后进入

False

您需要

else:
   print("number not between 1 and 10")

或更短

if guess>=1 and guess=<10:

您在三个地方使用if 1 <= guess =< 10:

int()

,但是您只能执行一次。

您应该在guess = int(input("Guess a number between 1 and 10: ")) try: guess = int(guess) if guess<1 or guess>10: count += 1 guess = int(guess)

内部进行操作
try/except

答案 1 :(得分:0)

对您的代码的评论:

  1. 将您的“输入语句”放入try ... except中。
  2. 仅一次使用int()
  3. 如果逻辑不正确
  4. 计数变量应为1而不为零
  5. 重复空的print()如果将其打印出额外的新行,则可以在上一个打印语句中添加'\ n'

请参阅以下有关其工作原理的mods:

import random
#Welcome Message
print("Welcome to my Guess the number program!\n")
number = random.randint(1, 10)
count = 1

while True:
    try:
        guess = int(input("Guess a number between 1 and 10: "))
        if guess>=1 and guess<=10:
            count += 1
            if guess < number:
                print("Too low.")
            elif guess > number:
                print("Too high.")
            else:
                print("You guessed it!")
                print("You guessed it in" ,count, "attempts.")
        else:
            print("number not between 1 and 10")
    except:
        print("Invalid input")