为什么我的脚本先打开然后不应该立即关闭? (蟒蛇)

时间:2018-07-13 04:05:34

标签: python if-statement

我正在尝试用python做一个更温暖/更寒冷的游戏。当前,当我打开程序时,尽管有多个循环和输入,它也会立即关闭。我在上面花了2个小时,我无法弄清楚。有提示吗?

import random
Correct = random.randint(1, 100)
Oldval = 50
Newval = 50
while Newval != Correct:
    Newval = Newval + input("How much do you want to move?")
    if (Newval - Correct) > (Oldval - Correct):
        print("Colder!")
    else:
        print("Warmer!")
    Oldval = Newval
Win = input(You Win!)

2 个答案:

答案 0 :(得分:0)

您试图添加一个字符串和一个整数。另外,您的最终输入在You Win!字符串周围没有引号。这是已修复问题的代码:

import random
correct = random.randint(1, 100)
oldval = 50
newval = 50
while newval != correct:
    newval = newval + int(input("how much do you want to move?"))
    if (newval - correct) > (oldval - correct):
        print("Colder!")
    else:
        print("Warmer!")
    oldval = newval
win = input("You Win!")

答案 1 :(得分:0)

您可以使用pylint工具来检测错误,例如缺少引号。 pylink test.py

E: 12, 0: invalid syntax (<string>, line 12) (syntax-error)

您可以使用pylint

安装pip install pylint