PyCharm错误,尝试和除外

问题描述 投票:-2回答:1

我为我的CS课写了一个小程序。我一直在撞墙,试图弄清楚为什么它不起作用。当我将代码粘贴到Trinket.io时,它有效!问题是try和except在PyCharm中仍然不起作用。我正在使用最新版本并验证它已设置为Python 3.只是寻找可能的原因和解决方案。我会尝试另一个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 ")
python-3.x exception-handling pycharm try-catch
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.