Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以让我的代码在多个平台上使用吗。_Python_Python 3.x - Fatal编程技术网

Python 我可以让我的代码在多个平台上使用吗。

Python 我可以让我的代码在多个平台上使用吗。,python,python-3.x,Python,Python 3.x,这是我用Python编写的一个短跑游戏的代码。你必须以最快的速度轻触“a”和“d”键才能跑100米**首先,我很感激任何人的意见,我也很想知道我是否可以修改我的程序,使其可以访问多个平台 import msvcrt import time high_score = 50 name = "no-one" while True: distance = int(0) print("\n---------------------------------------------------

这是我用Python编写的一个短跑游戏的代码。你必须以最快的速度轻触“a”和“d”键才能跑100米**首先,我很感激任何人的意见,我也很想知道我是否可以修改我的程序,使其可以访问多个平台

import msvcrt
import time
high_score = 50
name = "no-one"
while True:
    distance = int(0)
    print("\n--------------------------------------------------------------")
    print('\n\nWelcome to the 100m sprint, tap a and d rapidly to move!')    
    print('* = 10m')
    print("\n**Current record: " + str(high_score) + "s, by: " + name)
    print('\nPress enter to start')
    input()
    print('Ready...')
    time.sleep(1)
    print('GO!')
start_time = time.time()
while distance < 100:           
    k1 = msvcrt.getch().decode('ASCII')
    if k1 == 'a':
        k2 = msvcrt.getch().decode('ASCII')
        if k2 == 'd':
            distance += 1
            if distance == 50:
                print("* You're halfway there!")
                elif distance % 10 == 0:
                    print('*')
fin_time = time.time() - start_time
fin_time = round(fin_time,2)
print('Well done you did it in...'+str(fin_time))
if fin_time < high_score:
    print("Well done you've got a new high score ")
    name = input("Please enter your name : ")
导入msvcrt
导入时间
高分=50
name=“无人”
尽管如此:
距离=整数(0)
打印(“\n---------------------------------------------------------------------------------”)
打印(“\n\n欢迎参加100米冲刺,快速点击a和d移动!”)
打印('*=10m')
打印(“\n**当前记录:”+str(高分)+“s,按:”+name)
打印(“\n按enter开始”)
输入()
打印('Ready…'))
时间。睡眠(1)
打印('GO!')
开始时间=time.time()
距离<100时:
k1=msvcrt.getch().decode('ASCII')
如果k1=='a':
k2=msvcrt.getch().decode('ASCII')
如果k2=='d':
距离+=1
如果距离=50:
打印(“*您已经完成了一半!”)
elif距离%10==0:
打印(“*”)
fin\u time=time.time()-开始时间
fin_时间=整轮(fin_时间,2)
打印(‘做得好,你在…’+str(最后时刻))
如果fin_时间<高分:
打印(“干得好,你获得了一个新的高分”)
name=输入(“请输入您的姓名:”)

您的代码完全可以在任何具有Python解释器的平台上运行

是代码检查答案正确
msvcrt
是一个专门用于windows的模块,您可以在执行操作之前检查代码运行的操作系统,从而使代码跨平台运行。这可以通过
plaform
模块来完成

import platform
if platform.system() == "Windows":
    import msvcrt
    # do windows only stuff

if platform.system() == "Linux":
    # do some linux stuff

在代码审查中,我被告知:如果您不知道,您的代码是不跨平台的。这是因为msvcrt仅适用于MS Windows。要允许在Linux和OSX上使用,您应该使用sys、tty和termios,如本SO答案所示。这样做将允许更多用户使用您的程序。我认为我可以对其进行调整以使其更广泛地可用。从逻辑上讲,我没有意识到这就是
getch
的来源。您使用msvcrt的具体原因是什么?为什么不直接使用中的标准
getch
,老实说,我不确定,我所知道的只是它有效,所以我使用了它。哈哈sorry@Jaron在Pypi页面中:“平台:Linux/UNIX(Windows未知…)”显示了如何跨平台获取输入