Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python 3.4 randrange不想工作?_Python_Random - Fatal编程技术网

为什么Python 3.4 randrange不想工作?

为什么Python 3.4 randrange不想工作?,python,random,Python,Random,我目前正在为我和我的朋友制作一个小口袋妖怪游戏。但是为了做到这一点,我打算使用randrange模块来随机分配使用了哪些问题,而不是按顺序排列。我这样做了,但每次我运行程序时,它并没有做它需要做的事情,它只是坐在一个空白屏幕上。除此之外,我是否可以确保它每轮只使用一次数字?谢谢源代码: #All importations. import sys import os import time from random import randrange, uniform #Clear Screen de

我目前正在为我和我的朋友制作一个小口袋妖怪游戏。但是为了做到这一点,我打算使用randrange模块来随机分配使用了哪些问题,而不是按顺序排列。我这样做了,但每次我运行程序时,它并没有做它需要做的事情,它只是坐在一个空白屏幕上。除此之外,我是否可以确保它每轮只使用一次数字?谢谢源代码:

#All importations.
import sys
import os
import time
from random import randrange, uniform
#Clear Screen
def cls():
    os.system('cls')
#The Game
def game():
    score = 0
    possible_score = 20
    print ("In a moment, we will ask you a series of questions relating to Pokémon.")
    time.sleep(2)
    print ("These questions are in no certain order. Answer them the best you can.")
    time.sleep(2)
    print ("Good luck...")
    time.sleep(3)
    while True:
        irand = randrange(1, 2)
        if irand == '1':
            print ("What is the first Pokémon?")
            time.sleep(0.5)
            print ("A. Trashbag")
            time.sleep(0.5)
            print ("B. Bulbasaur")
            time.sleep(0.5)
            print ("C. Arceus")
            time.sleep(0.5)
            print ("D. Pikachu")
            time.sleep(1)
            question1 = input ("Your option: ")
            question1 = question1.lower()
            if question1 == 'b':
                score += 1
                time.sleep(1)
                print ("You got it right!")
            if question1 == 'a' or 'c' or 'd':
                time.sleep(1)
                print ("Sorry you answered incorrectly.")
        if irand == '2':
            print ("hai")


#Menu Code
def main():
    print ("Please select an option when prompted!")
    while True:  
        time.sleep(0.5)
        print ("[1] PokéTrivia Test")
        time.sleep(0.5)
        print ("[2] Credits")
        time.sleep(0.5)
        print ("[3] Exit")
        time.sleep(1)
        menu = input ("Please select an option: ")
        time.sleep(1)
        cls()
        #PokéTrivia Game
        if menu == '1':
            game()
        #Credits go to...
        if menu == '2':
            credit2()
        #Quit the game
        if menu == '3':
            print ("Exiting game...")
            time.sleep(1)
            break 
            SystemExit
#Startup Code
if __name__ == "__main__":
    print ("PokéTrivia!")
    time.sleep(1.5)
    print ("Developed by: Oiestin")
    time.sleep(2)
    print ("In partnership with:")
    time.sleep(1.5)
    print (" /---------\ ")
    print ("| oysterDev |")
    print (" \---------/")
    time.sleep(2)
    cls()
    main()
randrange工作正常,但返回一个整数。您正在测试字符串:

测试irand==1。您可能还希望增加最终值;它不包括在可能的值中。否则将只生成1。

randrange工作正常,但返回一个整数。您正在测试字符串:

测试irand==1。您可能还希望增加最终值;它不包括在可能的值中。否则将只生成1。

randrange将返回一个int

那么你说

if irand == '1':
因此,您正在比较int和str。您只需将语句更改为

if irand == 1:
randrange将返回一个int

那么你说

if irand == '1':
因此,您正在比较int和str。您只需将语句更改为

if irand == 1:

如果要包含端点,也可以使用random.randint。好的,谢谢。现在你知道如何使它每轮只使用一次整数了吗?@oiestin不知道你的意思是什么;您已经在while循环的每个迭代中生成了一个新值,尽管randrange1,2始终会生成1。使用randrange1,3也可以生成2,或者使用randint1,2,因为它的终点是包含的。@MartijnPieters我只想每轮使用一次数字2。我不希望它在一轮中重复。@oiestin:在这里定义“轮”。您可以选择何时调用randrange;每轮调用一次就可以了。如果要包含端点,也可以使用random.randint。好的,谢谢。现在你知道如何使它每轮只使用一次整数了吗?@oiestin不知道你的意思是什么;您已经在while循环的每个迭代中生成了一个新值,尽管randrange1,2始终会生成1。使用randrange1,3也可以生成2,或者使用randint1,2,因为它的终点是包含的。@MartijnPieters我只想每轮使用一次数字2。我不希望它在一轮中重复。@oiestin:在这里定义“轮”。您可以选择何时调用randrange;一轮打一次,你就没事了。