无法在python上续订随机数

无法在python上续订随机数,python,random,numbers,Python,Random,Numbers,我是python编程新手,我正在尝试做一个简单的命令行游戏。基本上,命令行要求从1到3的数字,用户给出数字,然后命令行将其与随机数进行比较,并决定胜利者是否通过,如果他通过洞,则重复该操作,直到他错过该数字。我的问题是为下一次更新随机数!每次命令行将它与用户的数字进行比较时,我都需要一个新的随机数 这是我的密码: #!/usr/bin/env python #ChooseADoor from random import randint import time import sys print

我是python编程新手,我正在尝试做一个简单的命令行游戏。基本上,命令行要求从1到3的数字,用户给出数字,然后命令行将其与随机数进行比较,并决定胜利者是否通过,如果他通过洞,则重复该操作,直到他错过该数字。我的问题是为下一次更新随机数!每次命令行将它与用户的数字进行比较时,我都需要一个新的随机数

这是我的密码:

#!/usr/bin/env python

#ChooseADoor

from random import randint
import time
import sys
print("Welcome mortal, to the Choose a door game...")
Score = 0
StrScore = str(Score)

#DeathDoor = 0
#DeathDoor = randint(1,3)



tutorialRequired = input("Would you like to go through the tutorial? (YES / NO): ")

if tutorialRequired == "YES":
    print("""Alright mortal, you were kidnaped and you wake up in a room.
There are 3 doors, two of them let you continue,
one of them gives you a painful death...
Yeah, sorry about that...
    """)
    ready = input("Ready mortal? (YES / NO): ")
    if ready =="YES":
        print("Starting game in 3...")
        time.sleep(1)
        print("2...")
        time.sleep(1)
        print("1...")
        time.sleep(1)
        print("GO!")
        time.sleep(1)
    else:
        print("Goodbye mortal.")
        sys.exit()
else:
    print("""Alright smarty pants let's see if you live to tell your story...
    """)
    ready = input("Ready mortal? (YES / NO): ")
    if ready =="YES":
        print("Starting game in 3...")
        time.sleep(1)
        print("2...")
        time.sleep(1)
        print("1...")
        time.sleep(1)
        print("GO!")
        time.sleep(1)
    else:
        print("Goodbye mortal.")
        sys.exit()


while Score < 10:

    print("Three doors ahed... ")
    door = input("Pick one (1, 2, 3): ")
    doorNum = int(door)


    if door in ("123"):
        DeathDoor = 0
        DeathDoor = randint(1,3)

        if DeathDoor == door:

            print("End of line, mortal.")
            print("You passed", StrScore, ("doors until the endo of your miserable life..."))

            break
        else:
            RandomNum = 0
            RandomNum = randint(1,3)
            if RandomNum == 1:
                print("You're safe for now, mortal. Go on.")
                DeathDoor = 0
                Score = (Score + 1)
                StrScore = str(Score)
                print("Score: " + StrScore)
            elif RandomNum == 2:
                print(" Beginner's luck. Let's see if you laugh next time mortal.")
                DeathDoor = 0
                Score = (Score + 1)
                StrScore = str(Score)
                print("Score: " + StrScore)
            elif RandomNum == 3:
                print(" Keep the fireworks, they may be useful for your funeral. Go on.")
                DeathDoor = 0
                Score = (Score + 1)
                StrScore = str(Score)
                print("Score:" + StrScore)

    else:
        print("Don't try to fool me,")
        print("goodbye mortal.")
        StrScore = str(Score)
        print("You passed " + StrScore + (" doors until the endo of your miserable life..."))
        time.sleep(1)
        sys.exit()

StrScore = str(Score)
print("Congratulations mortal, you live. For now."))
#/usr/bin/env python
#选择门
从随机导入randint
导入时间
导入系统
打印(“欢迎凡人,来选择门游戏…”)
分数=0
StrScore=str(分数)
#死亡门=0
#死亡门=randint(1,3)
tutorialRequired=input(“您想浏览教程吗?(是/否):”)
如果需要教程==“是”:
打印(“好吧,凡人,你被绑架了,你在一个房间里醒来。
有三扇门,其中两扇门让你继续,
其中一个会让你痛苦地死去。。。
是的,很抱歉。。。
""")
就绪=输入(“就绪凡人”(是/否):)
如果就绪==“是”:
打印(“在3中开始游戏…”)
时间。睡眠(1)
打印(“2…”)
时间。睡眠(1)
打印(“1…”)
时间。睡眠(1)
打印(“开始!”)
时间。睡眠(1)
其他:
打印(“再见,凡人。”)
sys.exit()
其他:
打印(“”“好吧,聪明的裤子,让我们看看你是否能活着讲述你的故事。。。
""")
就绪=输入(“就绪凡人”(是/否):)
如果就绪==“是”:
打印(“在3中开始游戏…”)
时间。睡眠(1)
打印(“2…”)
时间。睡眠(1)
打印(“1…”)
时间。睡眠(1)
打印(“开始!”)
时间。睡眠(1)
其他:
打印(“再见,凡人。”)
sys.exit()
分数<10分时:
打印(“三扇门啊……”)
门=输入(“选择一(1,2,3):”)
doorNum=int(门)
如果门处于(“123”):
死亡门=0
死亡门=randint(1,3)
如果死亡门==门:
打印(“行尾,凡人。”)
打印(“你通过了”,StrScore(“直到你悲惨生活结束的门…”)
打破
其他:
RandomNum=0
RandomNum=randint(1,3)
如果RandomNum==1:
打印(“你现在很安全,凡人。继续。”)
死亡门=0
分数=(分数+1)
StrScore=str(分数)
打印(“分数:+StrScore”)
elif RandomNum==2:
打印(“新手的运气。让我们看看你下次是否会笑,凡人。”)
死亡门=0
分数=(分数+1)
StrScore=str(分数)
打印(“分数:+StrScore”)
elif RandomNum==3:
打印(“保留烟花,它们可能对你的葬礼有用。继续。”)
死亡门=0
分数=(分数+1)
StrScore=str(分数)
打印(“分数:+StrScore”)
其他:
打印(“不要试图愚弄我,”)
打印(“再见,凡人。”)
StrScore=str(分数)
打印(“你通过了”+StrScore+(“直到你悲惨生活结束的门…”)
时间。睡眠(1)
sys.exit()
StrScore=str(分数)
打印(“祝贺你,凡人,你还活着。现在。”)

我做错了什么?

实际上,每个循环都会得到一个新的随机数。你的问题是:

...
if DeathDoor == door:
...
您将整数与字符串进行比较,结果总是
False
。正确的方法是

if DeathDoor == int(door)
同样,将
RandomNum
DeathDoor
重置为零也是不必要的


<>因为你刚刚开始学习Python,你应该考虑阅读Python风格指南。如果您从一开始就遵循指南中的规则,您的代码将更容易理解(不仅对其他人如此,对您也是如此)。

非常感谢!是的,我也认为我的代码有点乱,但我只是不知道什么是正确的方式,谢谢你的链接了!