我正在嘗試處理這個示例問題,詢問用戶他們想要生成多少個電源球票,然後它只爲他們做。如何根據用戶輸入循環x次數
到目前爲止,我有以下幾點:
import random
first_five_nums = range(1, 54) # first five number possibilities
powerball_nums = range(1, 43) # powerball number possibilities
num_to_select = 5 # to select 5 random numbers for the ticket
five_generated_nums = random.sample(first_five_nums, num_to_select) # first five numbers
powerball_generated = random.choice(powerball_nums) # powerball number
print ("Please enter the amount of Powerball tickets you would like to be generated..")
num_tickets = int(raw_input("Number of tickets: "))
for i in num_tickets():
print ("Your numbers: %s, Powerball: %s") % (five_generated_nums, powerball_generated)
當我嘗試運行此,我得到了以下錯誤:
Traceback (most recent call last):
File "D:\aa_grimes\my_python_works\powerball_generator.py", line 13, in <module>
for i in num_tickets():
TypeError: 'int' object is not callable
我有點新的Python,和避風港」除了wh以外,還有很多指導在我設法學習的過程中,出於這個原因,我完全陷入了僵局。我會感謝任何人的幫助。謝謝!
後,我試過了,我得到:'類型錯誤: '詮釋' 對象不iterable' – Nick
對不起,'因爲我在範圍內(num_tickets)',在範圍內當然 – schwobaseggl
嘗試'因爲我的(num_tickets) '。 –