2016-04-19 41 views
0

我在編碼的第一年。我正在編寫一個我目前遇到問題的學校計劃。該任務是使用文件創建一個瑣事遊戲。在我詢問用戶輸入以確定他們被問到的問題值多少分之後,我的代碼將不會繼續。具有問題的代碼部分直接在下面發佈。我的整個計劃都低於這個水平,以幫助任何人需要看到整個事情。只是爲了記錄整個程序尚未完成。用戶提供輸入後代碼未運行問題

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 
+0

這是什麼語言?你應該用這個信息來標記這個問題,因爲專家傾向於過濾問題。 –

回答

0

對於Python2,輸入()可能無法正常工作。如果是這種情況,請使用raw_input()。其次,輸入是一個字符串,但你將它與一個整數進行比較。 (1是0 + 1或2-1,但是「1」是一個棒,「2」是看起來像天鵝游泳到左邊的像素的負載,可以將它比較爲「200」或比較int(desired_value)用200 此外,你可能想看看開關(),的情況下,默認爲這個或,因爲代碼是在每種情況下幾乎相同,最小/最大值的關聯數組:

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. 

比你問但這些校長建議立即進行刪除多一點當你學習編碼時,d可以幫助你。祝你好運,享受!

+0

對於任何未來的問題,您最好標記「Python」來指定語言。 – Mike