繁体   English   中英

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

[英]random.shuffle() not working for number guessing game

我正在制作一个基于文本的冒险游戏,但有一次我希望用户无休止地输入数字。 我很新,python,所以越简单越好,但无论如何,这就是我到目前为止所拥有的。

这是代码

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!

正如评论中所指出的,您的程序的问题在于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")

暂无
暂无

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

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