使用用户输入控制随机数生成器中的参数

时间:2014-11-22 07:25:50

标签: python for-loop user-input

我正在尝试编写一个生成随机数的程序,询问你有多大,例如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()

1 个答案:

答案 0 :(得分: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)