random.shuffle()不适用于数字猜谜游戏

时间:2015-10-07 22:22:41

标签: python random numbers generator python-3.4

我正在制作基于文本的冒险游戏,但有一次我希望用户无休止地键入数字。我是一个非常新的蟒蛇,所以越简单越好,但到目前为止我已经得到了。

这是代码

import os
import sys
import string

import time
import random
numbers = ['1', '2', '3', '4', '5', '6', '7', '8' , '9']

print("Welcome to My Text Adventure Game: Dumb Edition")
print ("You (yes YOU) awake in an E-M-P-T-Y room.")
print ("It's very chilly willy and someone is drawing on the walls")
print (" Do you exit the room via the door that is obviously the right way (1)")
print ("what do you do?")
time.sleep(1)
print("Wait!")
print("You can't be trusted to not get this wrong")
print("I'll do it")
print("1")
time.sleep(1)
print("Erm...It should be going..")
print("Meh I'll just make something up! Two seconds..")
time.sleep(3)
print ("Ok.. I've got it! You'll just press buttons and it will be fun!")
print ("Or else..")
print ("Here we go...")
time.sleep(1)
print ("Please enter the following")
while True:
        rng_number = random.shuffle(numbers)
        print (rng_number)
        user_input = input("Go on, type it in!")
        if user_input == rng_number:
                print("Good job again!")
        else:
                print("Try again...Moron")

以下是我运行代码时发生的事情

Welcome to My Text Adventure Game: Dumb Edition
You (yes YOU) awake in an E-M-P-T-Y room.
It's very chilly willy and someone is drawing on the walls
 Do you exit the room via the door that is obviously the right way?(1)
what do you do?
Wait!
You can't be trusted to not get this wrong
I'll do it
1
Erm...It should be going..
Meh I'll just make something up! Two seconds..
Ok.. I've got it! You'll just press buttons and it will be fun!
Or else..
Here we go...
Please enter the following
None
Go on, type it in!None
Try again...Moron
None
Go on, type it in!

1 个答案:

答案 0 :(得分:0)

正如评论中所指出的,您的计划存在的问题是random.shuffle()未向rng_number分配号码。要将numbers的值分配给rng_number,您必须改为使用random.choice()Docs)。

import random
numbers = ['1', '2', '3', '4', '5', '6', '7', '8' , '9']

while True:
    rng_number = random.choice(numbers)
    print (rng_number)
    user_input = input("Go on, type it in!")
    if user_input == rng_number:
        print("Good job again!")
    else:
        print("Try again...Moron")
相关问题