2013-12-08 5 views
0

Python 놈의 비트이며 내 코드에 정규 표현식을 추가해야하지만 제대로 작동하지 않습니다./Google에서 오류 메시지를 검색했습니다. 틀린. 그러나 행운이 아닌 것을 계산하려고 노력했다. 그래서 나는 그 다음으로 할 일이 직접적으로 물을 것이라고 생각했다. 그래서 num()이 유효,TypeError : 'int'객체를 호출 할 수 없습니다. Python

# allows us to access a random 'key' in the dictionary 
import random 
import re 

# Contains the question and it's correct answer 
my_dict = { 
      "A form of Protectionism that imposes a tax on imports" : "tariff", 
      "....is quantity of a good or service that consumers are willing and able to  buy at a given price in a given time period" : "Demand", 
      "....is the quantity of a good or service that a producer is willing and able to supply onto the market at a given price in a given time period" : "Supply", 
      "By using ..... businesses can bring down their average costs by producing on a larger scale" : "Economies of scale", 
      "The cost of the next best alternative" : "Opportunity Cost", 
      ".... is the transfer of assets from the public (government) sector to the private sector." : "Privatisation" 
     } 


# welcome message 
print("Economics Revision App") 
print("=======================") 



# the quiz will end when this variable becomes 'False' 
playing = True 


# While the game is running 
while playing == True: 

    # set the score to 0 
    score = 0 

    # gets the number of questions the player wants to answer 
    num = int(input("\nHow many questions would you like: ")) 
    num = re.match("r\d[0-9]{2}$", num()) 
    if match: 
     print ('foo') 

    # loop the correct number of times 
    for i in range(num): 

     # the question is one of the dictionary keys, picked at random 
     question = (random.choice(list(my_dict.keys()))) 
     # the answer is the string mapped to the question key 
     answer = my_dict[question] 

     # print the question, along with the question number 
     print("\nQuestion " + str(i+1)) 
     print(question + "?") 

     # get the user's answer attempt 
     guess = input("> ") 

     # if their guess is the same as the answer 
     if guess.lower() == answer.lower(): 
      # add 1 to the score and print a message 
      print("Correct!") 
      score += 1 
     else: 
      print("Incorrect guess again!") 

    # after the quiz, print their final score 
    print("\nYour final score was " + str(score)) 

    # store the user's input... 
    again = input("Enter any key to play again, or 'q' to quit.") 

    #... and quit if they types 'q' 
    if again.lower() == 'q': 
     playing = False 

코드 나는 사투를 벌인거야 질문

+0

다음으로 가장 좋은 것은 예외의 스택 추적에서 오류가 발생한 행을 확인하는 것입니다. 'num()'이'num = re.match ("r \ d [0-9] {2} $", num())'줄에 무엇을 의미한다고 생각합니까? –

답변

1

num = re.match("r\d[0-9]{2}$", num())

num는 단순히 interger입니다과 : 다음은 지금까지 코드입니다.

match = re.match("r\d[0-9]{2}$", str(num))

1과 같은)가 바로 match해야 하는가?

2) restr에 노력하고, 이렇게 전달 된 인수는

str(num) 그런 다음 코드뿐만 아니라 벌금과 재미 있어야 있어야한다. :)

+0

감사합니다 정수를 accpets하지만 지금은 프로그램의 루프를 실행 못해? – user3080288

+0

@ user3080288 컴퓨터에서 실제로 제대로 작동합니다. 올바른 들여 쓰기를 따르고 있습니까? 질문에 편집 된 코드를 붙여 넣은 다음 버그를 수정 한 후에 실행하십시오. – Ray

+0

이제, ** ValueError : 밑줄 10을 사용하여 int()에 대한 리터럴이 잘못되었습니다 : 'g'**, 어떤 아이디어입니까? – user3080288

관련 문제