简体   繁体   English

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

[英]Why is my script is opening then closing immediately when it shouldn't? (Python)

I'm trying to make a warmer / colder game in python as a project. 我正在尝试使用python做一个更温暖/更寒冷的游戏作为一个项目。 Currently, when I open the program it closes immediately despite there being several loops and inputs. 当前,当我打开程序时,尽管有多个循环和输入,它也会立即关闭。 I have spent a good 2 hours on it, and I can't figure it out. 我在上面花了2个小时,我无法弄清楚。 Any tips? 有小费吗?

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!)

You were trying to add a string and an integer. 您试图添加一个字符串和一个整数。 Also, your final input didn't have quotes around the You Win! 另外,您的最终输入没有关于“ You Win!引号You Win! string. 串。 Here's the code with the issues fixed: 这是已修复问题的代码:

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!")

You could use pylint tool to detect the error like missing quotes. 您可以使用pylint工具检测错误,例如缺少引号。 pylink test.py

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

You could install pylint using pip install pylint 你可以安装pylint使用pip install pylint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM