【问题标题】:Why doesn't Python 3.4 randrange want to work? [closed]为什么 Python 3.4 randrange 不想工作? [关闭]
【发布时间】:2014-11-25 21:42:31
【问题描述】:

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

【问题讨论】:

    标签: python random


    【解决方案1】:

    randrange() 工作正常,但返回一个 整数。您正在测试 string

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

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

    【讨论】:

    • 如果要包含端点,也可以使用random.randint()
    • 好的,谢谢。现在你知道如何让它每轮只使用一次该整数了吗?
    • @oiestin 不确定您的意思;您已经在while 循环的每次迭代中产生了一个新值,尽管randrange(1, 2) 将始终产生1。使用randrange(1, 3) 也允许产生2,或者使用randint(1, 2),因为它的端点是包容性的。
    • @MartijnPieters 我只想每轮说一次数字 2。我不希望它在一轮中重复。
    • @oiestin:在这里定义“圆形”。您可以选择何时拨打randrange();一轮调用一次,你就没事了。
    【解决方案2】:

    randrange 将返回一个int

    那你说

    if irand == '1':
    

    因此,您将intstr 进行比较。您可以将语句更改为

    if irand == 1:
    

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 2015-03-19
      • 2012-11-09
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      相关资源
      最近更新 更多