当前位置: 首页 > 知识库问答 >
问题:

有问题与python输入

康文昌
2023-03-14

我对编程相当陌生,发现了一个让你制作游戏的练习。基本上,程序要求3个输入:停止、启动和退出。启动使汽车启动,停止使汽车停止,退出是不言自明的。但是,如果你在汽车已经启动时输入启动,它会告诉你汽车已经启动,依此类推。然而,当我将输入输入到终端时,没有显示任何内容。谁能告诉我我做错了什么?

这是我的密码:

started = False

carstate = str(input())

while carstate != "exit":
    if carstate == "start":
        if started == False:
            started == True
            print("Car has started... ready to go!")
        if started == True:
            print("Car has already started")
    elif carstate == "stop":
        if started == False:
            print("Car is already stopped!")
        if started == True:
            started == False
            print("Car is stopped")

if carstate == "exit":
    sys.exit()
if carstate == "help":
    print("Start - starts the car")
    print("Stop - stops the car")
    print("Exit - exits the game")

共有3个答案

吴修洁
2023-03-14

您当前的代码很可能会卡在time循环中。如果您通过输入start进入循环,您将不会打印任何内容,也没有任何机会更改该内容。

另一个注意事项是对exithelp的两个附加状态检查。我会在主while循环中包含这些(或至少是帮助一个)。

在两种情况下,我确信您希望使用赋值=,而不是等式检查=。还有其他一些清理工作:-)

这样做应该可以:

started = False

carstate = ""  # initialize empty, we will ask for input at every iteration

while carstate != "exit":
    carstate = input()  # ask what we want to do next
    if carstate == "start":
        if started == False:
            started = True  # here you want to make an assignment (use '=' not '==')
            print("Car has started... ready to go!")
        elif started == True:  # change to "elif", so it avoids double prints
            print("Car has already started")

    elif carstate == "stop":
        if started == False:
            print("Car is already stopped!")
        elif started == True:  # change to "elif", so it avoids double prints
            started = False  # here you want to make an assignment (use '=' not '==')
            print("Car is stopped")

    elif carstate == "help":
        print("Start - starts the car")
        print("Stop - stops the car")
        print("Exit - exits the game")

if carstate == "exit":
    sys.exit()

您可以在这里尝试:https://onlinegdb.com/S1gJ6lRGL

颛孙喜
2023-03-14

您在开始时要求用户输入,然后执行同时循环。因为你不会在这段时间再次要求不同的动作,所以它循环着“汽车开始了...准备出发!”。你必须在循环内做另一个"输入()"

姬凡
2023-03-14

欢迎来到StackOverflow!

这里的第一个问题是,由于carstate=input()不在while循环中,因此程序只调用用户输入一次。要多次请求用户输入,应将其放在如下位置:

carstate = ""

while carstate != "exit":
    carstate = input()

    # ...

因此,程序将继续要求用户输入。

第二个问题是==之间的区别=用于为变量赋值,而=用于将某个值与变量进行比较。所以,当你说started==True时,你真的应该使用=

此外,您应该将帮助块也放置在同时循环中,否则它将永远不会执行。

所以最后的代码应该是这样的:

import sys

started = False

carstate = ""

while carstate != "exit":
    carstate = input()
    if carstate == "start":
        if started == False:
            started = True
            print("Car has started... ready to go!")
        elif started == True:
            print("Car has already started")
    elif carstate == "stop":
        if started == False:
            print("Car is already stopped!")
        elif started == True:
            started = False
            print("Car is stopped")
    elif carstate == "help":
        print("Start - starts the car")
        print("Stop - stops the car")
        print("Exit - exits the game")

if carstate == "exit":
    sys.exit()

工作示例

 类似资料:
  • 我需要调用MySQL存储过程从我的python脚本。作为参数之一,我传递一个Unicode字符串(俄语),但我得到一个错误; UnicodeEncodeError:“latin-1”编解码器无法对位置0-1中的字符进行编码:序号不在范围(256)内 我的脚本: 我已经读过设置可以解决这个问题,但是当我使用字符串时: 这给了我另一个错误; UnicodeEncodeError:'utf-8'编解码器

  • 当我输入名字时,它会给我一个有效的数字或一个特殊字符。我想这样做,使数字不被考虑在内,不会被返回,或者它可能会给出一个错误消息。

  • 我的问题是,关闭库存甚至没有悄悄地工作。谁有更好的解决方案或解决方案?

  • 我试图重新大小的视频不在同一个目录的PHP exec代码,这是我的ffmpeg代码:

  • 问题内容: python GIL的存在是否意味着在python多线程中 相同的 操作与在单线程中重复操作有什么不同? 例如,如果我需要上传两个文件,那么在两个线程中执行而不是一个接一个地上传它们有什么好处? 我以两种方式尝试了一次大型数学运算。但是他们似乎花费几乎相同的时间才能完成。 这似乎对我来说还不清楚。有人可以帮我吗?谢谢。 问题答案: Python的线程在说唱方面要比应有的差一些。在三种(

  • 总的来说,我的问题是我试图用WinnAppDriver自动化Windows 10应用程序,并用Python编写我的自动化程序。似乎很少有关于如何做到这一点的信息,因为大多数WinAppDriver示例都是用Java编写的(可以理解...).但是我对Python基本一无所知,对Java更是一无所知。因此...我错过了什么书或视频吗?具体来说,我安装了所有的东西(通过VS代码安装Python ),并实