簡體   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