2013-12-08 37 views
0

python noob的位和我需要在我的代码中添加正则表达式,但无法让它工作:/我搜索了谷歌的错误消息和试图找出什么是错的,但没有运气,所以我想下一个最好的事情是直接问。 这是迄今为止代码: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仅仅是一个整数,所以num()是无效的。

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

1)它应该是match吧?

2)re正在str,所以传递的参数应该是str(num)

那么代码应该是罚款和乐趣,以及。 :)

+0

感谢它认为整数,但现在它不会运行该程序的循环? – user3080288

+0

@ user3080288它在我的电脑上效果很好。你是否遵循正确的缩进?考虑在您的问题中粘贴编辑后的代码,并在修复错误后运行它。 – Ray

+0

现在即时获取,** ValueError:无效的文字为int()与基地10:'g'**,任何想法? – user3080288