为什么Python 3.4 randrange不能工作?

时间:2014-11-25 21:42:31

标签: python random

我正在为我和我的朋友制作一个小型口袋妖怪游戏。但要做到这一点,我打算使用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()

2 个答案:

答案 0 :(得分:3)

randrange()工作正常,但返回整数。您正在测试字符串

irand = randrange(1, 2)
if irand == '1':

测试irand == 1。您可能还想增加最终值;它是包含在可能的值中。否则只会生成1

答案 1 :(得分:1)

randrange将返回int

然后你说

if irand == '1':

因此,您要将intstr进行比较。您只需将语句更改为

即可
if irand == 1: