PyCharm错误,尝试并除外

时间:2018-08-29 20:12:46

标签: python-3.x exception-handling pycharm try-catch

我为CS课编写了一个小程序。我一直把头撞在墙上,试图弄清楚为什么它不起作用。当我将代码粘贴到Trinket.io时,它起作用了!问题在于,try和except在PyCharm中仍然不起作用。我正在使用最新版本,并确认已将其设置为Python3。只是在寻找可能的原因和解决方案。我会尝试另一个IDE,但该课程要求使用PyCharm,而这正是教授用来对项目进行评分的方式。谢谢。

# This is a guess the number game using the random module
from random import randint
import sys

randomnum = randint(1, 101)  # set random number between 1 and up to but not including 101
guesses = 0  # Starting Value for Guesses

while guesses != randomnum:  # while loop to allow for multiple guesses
    guesses = input("I am thinking of a number between 1 and 100, Take A Guess: ")  # get users guess

    try:
        guess = int(guesses)  # Convert Guesses to An Integer

    except ValueError:
        print("You Did Not Enter A Valid Number! ")
        sys.exit()

    if guesses == randomnum:  # If player gets it correct
        print("You Guessed My Number !!! ")

    elif guesses < randomnum:  # Tells the user their guess was too low
        print("Too Low ")

    elif guesses > randomnum:  # Tells the player their guess was too high
        print("Your Guess is Too High ")

1 个答案:

答案 0 :(得分:0)

尝试下载最新的python版本(我认为是3.7),然后确保为项目设置了正确的python解释器。如果您需要更多帮助,请发表评论!