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

问题描述 投票:0回答:2

我有一个猜测游戏,我想将用户输入验证为数字,数字必须在我的范围之间(如果这里甚至是正确的术语),如果没有,则返回允许用户再次尝试的错误消息。

我已经设置了我的猜谜游戏,但现在我正试图设置它的限制。我尝试使用isdigit(str),但我不确定我是否完全理解它是如何工作的所以我现在有点困难,试图让它与TRY和Except一起工作:我也认为我搞砸了我的代码之前有效,因为现在当我输入5时,它说数字不在1到10之间,所以我的其他说法是错误的。

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
python
2个回答
0
投票

这是错的

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里面做

guess = input("Guess a number between 1 and 10: ")
try:
    guess = int(guess)
    if 1 <= guess =< 10:
        count += 1

0
投票

对您的代码的评论:

  1. 把你的“输入声明”放在try ...中除外。
  2. 仅使用一次int()
  3. 如果逻辑不正确
  4. count变量应该是一个而不是零
  5. 重复空print()如果你打印额外的新行你可以添加'\ n'到上一个print语句

在你的代码上看到以下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")
© www.soinside.com 2019 - 2024. All rights reserved.