简体   繁体   中英

random.shuffle() not working for number guessing game

Im making a text based adventure game but at one point I want the user to be stuck typing numbers endlessly. I'm very new and python so the simpler the better but here's what i've got so far anyway.

Here is the code

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")

Here is what happens when I run the code

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!

As pointed out in the comments, the issue with your program is that random.shuffle() does not assign a number to rng_number . In order to assign a value from numbers to rng_number , you must use random.choice() instead ( 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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