Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我目前正在为我和我的朋友制作一个小口袋妖怪游戏。但是要做到这一点,我将使用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 = randrange(1, 2)
if irand == '1':


测试irand == 1代替。您可能还希望增加最终价值。它不包括在可能的值中。否则只会产生1

关于python - 为什么Python 3.4 randrange不想要工作? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27137277/

10-11 20:50