【问题标题】:Issue with code not running after user provides input用户提供输入后代码未运行的问题
【发布时间】:2016-08-12 08:36:03
【问题描述】:

我现在是编码的第一年。我正在为我目前遇到一些问题的学校编写程序。任务是使用文件创建一个琐事游戏。在我要求用户输入以确定他们被问的问题的价值后,我的代码将不会继续。有问题的代码部分直接贴在下面。我的整个程序都低于该程序,以防万一任何人需要查看整个程序。只是为了记录整个程序还没有完成。

P.S 我是新手,所以如果我遗漏了一些明显的东西,我深表歉意。

while repeat3==True:
    #identifying what point value the player wants        
    print "what point value do you want the question to have?"
    print "your options are:200,400,600,800,1000"
    desiredValue=input()
    print 'testing'
    if desiredValue==200:
        questionValue=random.randint(1,5)
        repeat3=False
    elif desiredValue==400:
        repeat3=False
        questionValue=random.randint(5,9)
    elif desiredValue==600:
        repeat3=False
        questionValue=random.randint(8,13)
    elif desiredValue==800:
        repeat3=False
        questionValue=random.randint(13,17)
    elif desiredValue==1000:
        repeat3=False
        questionValue=random.randint(17,20)
    else:
        print 'please entre one of the good values'
#asking the user the question        
print "Here is the question:"
print temporaryQuestions[currentCategory][questionValue]

这是我到目前为止的整个程序

#quiz Master Project
#imports
import random
import time
#variables defined
categorys=["history", "vintage tv", "harry potter", 'mythology']
questionFiles=['history questions.txt','vintage tv show 

questions.txt','HarryPotterQuestions.txt','mythQuestions.txt']
answerFiles=['history answers.txt','vintageTVAnswers.txt','HarryPotterAnswers.txt','mythAnswers.txt']
chosenCategory=0
HistoryQuestionsList=[]
TVQuestionsList=[]
HarryPotterQuestionsList=[]
MythQuestionsList=[]
temporaryQuestions=[HistoryQuestionsList,TVQuestionsList,HarryPotterQuestionsList,MythQuestionsList]


repeat1=True
repeat2=True
repeat3=True
desiredValue=0
name=0
# functions


#_______________________________________________________________________________
#turning the questions into lists
#history questions
a= open ('history questions.txt','r')
reader1=a.readlines()
for line1 in reader1:
    HistoryQuestionsList.append(line1)
#vinatage tv
b= open('vintage tv show questions.txt','r')
reader2=b.readlines()
for line2 in reader2:
    TVQuestionsList.append(line2)
#Harry potter
c=open('HarryPotterQuestions.txt','r')
reader3=c.readlines()
for line3 in reader3:
    HarryPotterQuestionsList.append(line3)
#Mythology
d=open('mythQuestions.txt','r')
reader4=d.readlines()
for line4 in reader4:
    MythQuestionsList.append(line4)

#prompting
print "hello and welcome to (for copyright reasons) japordy!"
print 
print "what is your name?"
name=raw_input()
print 
print "you are going to be able to chose from a few types of questions to answer"
time.sleep(.2)
print
print "you will be asked 10 questions"
time.sleep(.2)
print 
print "first you need to decide what catagory to get a question from then, select a question on the basis of points."
time.sleep(.2)
print 
print "you may chose from a different catagory each time"
time.sleep(.2)
print
print "after you are asked a question that question will be deleted from the questions that you can possibly get"
time.sleep(.2)
print 
print "the point system works like this, if you get a question right you will be given the total number of points that question was worth."
print "But if you get the question wrong you will be fined the number of points that the question was worth."
print "if you take to long to answer you will not get a chance to answer and you will not recieve or be fined points"
time.sleep(3)
print
print "the catagories that you can chose from are: history, vintage tv, harry potter, and mythology"
time.sleep(.2)
print
print "the point values are 200, 400, 600, 800, and 1000"
print
#selecting the questions that will be asked
print "please entre the catagory you want to choose"
chosenCategory=raw_input()
chosenCategory=chosenCategory.lower()#converting all the letters to lowercase
#seeing if the user entered a valid category and if so creating a file that can be used for each round. 
while repeat1==True:
    while repeat2==True:
        for i in range(1,5):
            if chosenCategory==categorys[i-1]:
                currentCategory=i-1
                repeat2=False
        if repeat2!=False:
            #selecting the questions that will be asked
            print "the catagories that you can chose from are: history, vintage tv, harry potter, and mythology"
            print
            print "please entre the catagory you want to choose"
            chosenCategory=raw_input()
            chosenCategory=chosenCategory.lower()#converting all the letters to lowercase
    while repeat3==True:
        #identifying what point value the player wants        
        print "what point value do you want the question to have?"
        print "your options are:200,400,600,800,1000"
        desiredValue=input()
        print 'testing'
        if desiredValue==200:
            questionValue=random.randint(1,5)
            repeat3=False
        elif desiredValue==400:
            repeat3=False
            questionValue=random.randint(5,9)
        elif desiredValue==600:
            repeat3=False
            questionValue=random.randint(8,13)
        elif desiredValue==800:
            repeat3=False
            questionValue=random.randint(13,17)
        elif desiredValue==1000:
            repeat3=False
            questionValue=random.randint(17,20)
        else:
            print 'please entre one of the good values'
    #asking the user the question        
    print "Here is the question:"
    print temporaryQuestions[currentCategory][questionValue]
    UserAnswer=raw_input

【问题讨论】:

  • 这是什么语言?您应该使用该信息标记此问题,因为专家倾向于过滤问题。

标签: file input while-loop


【解决方案1】:

对于 Python2,输入 () 可能不起作用。如果是这种情况,请使用 raw_input()。其次,输入是一个字符串,但您正在将它与一个整数进行比较。 (1 是 0+1 或 2-1,但“1”是一根棍子,“2”是看起来像向左游动的天鹅的像素负载。您可以将其与“200”进行比较或比较 int(desired_value) 200。 此外,您可能想查看 switch(),case,default 或者,因为代码在每种情况下几乎相同,所以最小/最大值的关联数组:

randMinMax = {200: {1,5}, 400: {5,9} ...}
while True:         #infinite loop - see 'break' below
    desired_input = int(raw_input)    #Might want to try/catch in case of bad input
    if desired_input in randMinMax:
        questionValue=random.randint(randMinMax [desired_input][0], randMinMax [desired_input][0])
        break;          # Break out of loop
    else:
        print 'please entre one of the good values'

但是,在这种情况下还有一个更快的版本,因为输入值和随机限制之间存在固定关系,因此:

limit = (desired_input / 200) - 1     # 0 to 4 inclusive
if limit==int(limit) and limit < 5:   # not, for example, 215
    limit = (limit * 4) + 1           # 1, 5, 9, 13, 17
    questionValue=random.randint(limit,math.min (limit+4, 20))   # Because the last limit is 20, not 21 - hence math.min
    break;                            # Got a result, stop looping.

比您要求的要多一点,但这些原则应该可以帮助您学习编码。祝你好运,尽情享受!

【讨论】:

  • 对于未来的任何问题,您最好标记“Python”以指定语言。
猜你喜欢
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多