Python猜测游戏拒绝无效的用户输入

时间:2018-05-25 15:24:43

标签: python

我正在使用我的第一个Python类,并且像大多数Python类一样,最后的任务是创建一个1-100的猜谜游戏,跟踪VALID尝试的次数。我无法获取的元素(或在stackoverflow上找到)是如何拒绝无效的用户输入。用户输入必须是1到100之间的整数,正数。我可以让系统拒绝除0和< + 101之外的所有内容。

我能想到的唯一事情就是告诉我你不能让运算符比较字符串和整数。我一直想用猜猜> 0和/或猜测< 101.我也试图创造某种功能,但无法让它正常工作。

#  Generate random number
import random
x = random.randint(1,100)

#  Prompt user for input
print("I'm thinking of a number from 1 to 100")
counter = 0

while True:
    guess = input("Try to guess my number: ")

    #  Check if input is a positive integer and is not 0 or >=101
    #  this line doesn't actually stop it from being a valid guess and 
    #  counting against the number of tries.
    if guess == "0":  
        print(guess, "is not a valid guess")
    if guess.isdigit() == False: 
        print(guess, "is not a valid guess")
    else: 
        counter += 1
        guess = int(guess)

        #  Begin playing
        if guess > x:
            print(guess, "is too high.")
        elif guess < x:
            print(guess, "is too low.")
        else:
            print(guess, "is correct! You guessed my number in", counter, "tries!")

3 个答案:

答案 0 :(得分:-1)

import random
x = random.randint(1,100)

#  Prompt user for input
print("I'm thinking of a number from 1 to 100")
counter = 0

while True:
    guess = input("Try to guess my number: ")
    try:
        guess = int(guess)
        if(100 > guess > 0):
            counter += 1
            guess = int(guess)
            #  Begin playing
            if guess > x:
                print(guess, "is too high.")
            elif guess < x:
                print(guess, "is too low.")
            else:
                print(guess, "is correct! You guessed my number in", counter, "tries!")
                break
        else:
            print("Number not in range between 0 to 100")
    except:
        print("Invalid input")

答案 1 :(得分:-1)

#  Generate random number
import random
x = random.randint(1,100)

#  Prompt user for input
print("I'm thinking of a number from 1 to 100")
counter = 1   


while True:
    try:
        guess = int(input("Try to guess my number: "))
        if guess > 0 and guess < 101:
            print("That's not an option!")
        #  Begin playing
        elif guess == x:
            print(guess, "is correct! You guessed my number in", counter, "tries!")
            break
        elif guess > x:
            print(guess, "is too high.")
        elif guess < x:
            print(guess, "is too low.")
        else:
            counter += 1        
    except:
        print("That's not a valid option!")

答案 2 :(得分:-1)

我的导师帮助了我。 (我发布是为了避免让那些给我评分的人。)这就是我们想出来的。我发布它是为了帮助任何可能有这种特殊拒绝用户输入问题的未来Python学习者。

谢谢你们发布SO FAST!即使我需要教练的帮助,如果没有你的见解,我会更加无能为力。现在我可以享受我的假期周末。祝阵亡将士纪念日周末!

import random
x = random.randint(1,100)

print("I'm thinking of a number from 1 to 100.")
counter = 0

while True:
    try:        
        guess = input("Try to guess my number: ")
        guess = int(guess) 
        if guess < 1 or guess > 100:
            raise ValueError()
        counter += 1
        if guess > x:
            print(guess, "is too high.")
        elif guess < x:
            print(guess, "is too low.")
        else:
            print(guess, "is correct! You guessed my number in", counter, "tries!")
            break
    except ValueError:
        print(guess, "is not a valid guess")