[英]TypeError: 'int' object is not callable In Python
python noob的一點,我需要在我的代碼中添加一個正則表達式,但無法使其正常工作:/我已經在Google上搜索了錯誤消息,並試圖找出問題所在,但是沒有運氣,所以我想出了下一個最好的方法要做的就是直接問。 這是到目前為止的代碼:
# 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
我在問題中苦苦掙扎的代碼
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)
然后,代碼也應該很好玩。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.