2014-11-22 34 views
0

我試圖編寫一個程序,產生隨機數,詢問你想要它有多大,例如1-4之間,然後我想問它你有多少個數字希望並最終問你是否要再次運行它。我試圖掌握遞歸和類型轉換的概念,只是試圖用Python學習面向對象設計的概念。我試圖從迄今爲止在Learn Python Hard Way網站上閱讀的內容中收集。使用用戶輸入來控制隨機數發生器

from random import randint 
def random_with_N_digits(n): 
    range_start = 10**(n-1) 
    range_end = (10**n)-1 
    return randint(range_start, range_end) 

# Here I am attempting to define the variables I need to convert keyboard input to int 

size = raw_input() 
intsize = # unsure of how to define this variable 
intsize = int(size) 

print "How many Digits?", 
size = raw_input() 

print "How many Times?". 
times = raw_input() 

# I want to construct a method here that sends my size input to random_with_N_digits(n) 
# I need a method that runs the random_with_N_digits according to the input. 
# while True: 
reply = input('Enter text:') 
if reply == 'stop': 
    break 
    if reply = times # not sure what to put here 
     print(def random_with_N_digits) 
    else: 
     # I want to tell the my method to run as many as much as 'times' is equal to 


print "Again?" 
answerMe = raw_input() 

# here I want to have a if answerMe = Enter run the method again 


print random_with_N_digits() 

回答

0

試試這個代碼

from random import randint 
def random_with_N_digits(n): 
    s = int(raw_input("starting from.. ")) 
    e = int(raw_input("upto... ")) 
    l = "" 
    for i in range(0,n): 
     l= l +str(randint(s,e)) 
    print int(l) 
    again = raw_input("print again?") 
    if again == "yes": 
     how_many =int(raw_input("how many digits?")) 
     random_with_N_digits(how_many) 

how_many =int(raw_input("how many digits?")) 
random_with_N_digits(how_many)