我应该如何在我的 for 循环中添加 Try 和 except Block 来处理

How should I add Try & except Block to handle in my for loop

        # This is a guess the number game.
        import random

        print ('Hello, What is your name?')
        name = input()

        print('Well, ' + name + '! I am thinking of a number between 1 to 20.') 
        secretNumber = random.randint(1,20)
        print('Debug:' + str(secretNumber))

        for guessTaken in range (1,7):
            print('Take a guess. '+ name +'!' )
            guess =int(input())


            if guess<secretNumber:
                print('Your guess is too low')
            elif guess>secretNumber:
                print('Your guess is too high')
            else:
                break # This is for correct guess is equal.

        if guess ==secretNumber:
            print('Good Job, ' +name+ '! You guess my number in ' + str(guessTaken)+ ' guesses!')
        else:
            print('Nope, The number I was thinking of was ' +str(secretNumber))

嗨,我的超级程序员们,

所以我想把 Try 和 Except 块放在这个程序中,我试着把它放在后面 guess=int(input()) 不幸的是我无法让它工作。

所以我正在尝试处理 ValueError,所以假设用户需要输入整数(数字),如果他键入 "One"、"Six" 等字符串,程序就会崩溃。我想处理这个案子。请帮帮我。 :)

非常感谢。 眼镜蛇

怎么样,它会重新询问用户是否输入字符串:

        # This is a guess the number game.
        import random

        print ('Hello, What is your name?')
        name = input()

        print('Well, ' + name + '! I am thinking of a number between 1 to 20.') 
        secretNumber = random.randint(1,20)
        print('Debug:' + str(secretNumber))

        for guessTaken in range (1,7):
            print('Take a guess. '+ name +'!' )
            try:
               guess =int(input())
            except:
               print("Must be a number, try again: ")
               continue

            if guess<secretNumber:
                print('Your guess is too low')
            elif guess>secretNumber:
                print('Your guess is too high')
            else:
                break # This is for correct guess is equal.

        if guess ==secretNumber:
            print('Good Job, ' +name+ '! You guess my number in ' + str(guessTaken)+ ' guesses!')
        else:
            print('Nope, The number I was thinking of was ' +str(secretNumber))

如果用户输入的不是数字 python 将跳到异常,因为会发生错误,我在打印件中添加了一些文本以阐明游戏规则。 我在 for 循环之后使用了 else 这对于用户在 7 次尝试中无法猜出数字的情况有时非常有用你可以测试它并看到即使 else 不在 if 之后,当 if guess==secretNumber是真的,你打破了循环,else 语句没有做任何事情。 P.S 在这里回答还很陌生,所以请对我的回答提供反馈

import random

print ('Hello, What is your name?')
name = input()

print('Well, ' + name + '! I am thinking of a number between 1 to 20.') 
secretNumber = random.randint(1,20)
print('Debug:' + str(secretNumber))

for guessTaken in range (1,7):
    print('Take a guess. '+ name +'!, You Have 7 tries'  )
    try:
        guess = int(input())
        if guess<secretNumber :
            print('Your guess is too low')
        elif guess>secretNumber : 
            print('Your guess is too high')
        if guess == secretNumber:
            print('Good Job, ' + name + '! You guess my number in ' + str(guessTaken)+ ' guesses!')
            break # This is for correct guess is equal.
    except:
        print('Please Enter A number ' + name + 'and not anything else' )


else:
    print('Nope, The number I was thinking of was ' +str(secretNumber))