Python IDLE 和 VS Code python 解释器的不同结果

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

在 Python IDLE 中执行以下代码,它按预期工作

jump = 623
counter = 0
temp = 0

while True:
    try:
        number = int(input("Enter the number: "))
    except ValueError:
            print("Sorry i didnt understand that.")
            continue

    if number < 623:
        print("Number input should be more than 623")
        continue
    elif number < 1920:
        print("Number input should be less than 1920")
        continue
    else:
        break

for x in range(0, number, jump):
    counter+=1
    print(counter)

输出:

Enter the number: abc
Sorry i didnt understand that.
Enter the number: 

但是在 VS 代码中它给出了一个错误:[CHECK SCREENSHOT]enter image description here

Exception has occurred: ValueError
invalid literal for int() with base 10: 'abc'

我的 Python IDLE 版本是

3.10.30
而对于 VS 代码中的 python 解释器
3.10.5

(我期待类似的结果)

python visual-studio-code exception python-idle
1个回答
0
投票

代码不应该打印计数循环迭代次数的计数器变量,而是打印 x 的值,它是跳转变量的当前倍数。

所以循环的正确代码将是…………

for x in range(0, number, jump): 打印(x) 计数器 += 1

这将打印出 jump 变量在 0 和输入数字之间的所有倍数,并计算打印的倍数。

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